【问题标题】:load a different nib if phone is not connected to internet如果手机未连接到互联网,请加载不同的笔尖
【发布时间】:2011-11-13 13:46:12
【问题描述】:

我使用 Apple 的 Reachability 类,它使用警报告诉用户连接不可用或连接丢失,它工作正常。但是,我想将警报更改为更直观的东西。我想加载一个笔尖,告诉用户没有活动连接,但笔尖没有加载。我也尝试加载我的其他笔尖,但它也没有加载笔尖。

- (BOOL) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
    case NotReachable:
    {
        NSLog(@"The internet is down.");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Internet Connection" message:@"You are currently not connected to a WI-FI or cellular Data.\nPlease make sure you are connected." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];

        //NoConnection *noConn = [[NoConnection alloc] initWithNibName:@"NoConnecton" bundle:nil];
        //[self presentModalViewController:noConn animated:NO];
        //[NoConnection release];

        self.isConnected = NO;
        return NO;
        break;

    }
    //more cases.........

警报部分工作正常,但加载笔尖的部分却不行。你能告诉我这里有什么问题吗?我在 viewWillAppear 中调用了这个函数。谢谢!

【问题讨论】:

  • 代码是否可以在 viewwillapper 等其他任何地方工作?
  • 我在 viewDidDLoad 中尝试过,但也没有用。我收到了警报,但没有收到笔尖。
  • 很明显你的 nib 文件有问题 打开 nib 文件并查看是否一切正常,特别是查看文件的新所有者类,在自定义类中,
  • 如果这不起作用并且您赶时间,您可以创建一个不同的视图来打开,而不是使用不同的 nib 文件打开相同的视图。这不是很推荐,但应该可以工作
  • 试试这个 [self.navigationController presentModalViewController:detailView animated:YES];

标签: iphone objective-c ios reachability


【解决方案1】:

您可以执行以下操作:

if ( ! isConnected )
{
    NoConnection *noConn = [[NoConnection alloc] initWithNibName:@"NoConnecton" bundle:nil];
    [self presentModalViewController:noConn animated:NO];
    [NoConnection release];
}

【讨论】:

  • 我已经有了,但它没有显示,我只是评论它以通过警报测试。
  • 不要在checkNetworkStatus:中使用它,在其他方法中使用它,可能是IBAction/按钮按下,或者viewWillAppear:
【解决方案2】:

您提供的代码应该可以工作,因此问题可能出在 nib 的其他地方 - 链接,您可能忘记将某些内容链接到 nib 文件。 试试这个

    [self.navigationController presentModalViewController:noConn animated:YES];  

【讨论】:

  • NoConnecton 笔尖应该没问题。它只是一个包含图像的视图。我尝试在我的其他课程中调用我的 NoConnecton 笔尖,它加载得很好,所以笔尖没问题吧?我现在所做的是我在同一个方法上加载了一个不同的笔尖(w/c 在我的其他类中加载得很好),但视图不会显示。
  • 仍然没有运气 [self.navigationController presentModalViewController:noConn animated:YES];
  • 你正在实现 presentModalView 调用的类是 UIVIewController 吗?
  • 是的,它是一个 uiviewcontroller。如果不是,那么它应该显示一个警告,但我在 uiviewcontroller 中实现它。
  • 我被踩得很奇怪的bug,
【解决方案3】:

你的笔尖是否有 NoConnection 作为文件的所有者(我猜 NoConnection 是 UIViewController 的子类,检查一下。我会在下面调用这个 NoConnectionViewController,因为你应该这样命名它不会出错)?

文件的所有者视图属性是否与图形视图相关联?检查。

您是否在窗口顶部没有状态栏的情况下工作?那可能是个问题。

你在 modalViewController 里面吗?如果是,您的代码将不起作用,您必须改用:

    NoConnectionViewController* nextWindow = [[NoConnectionViewController alloc]  initWithNibName:@"NoConnecton" bundle:nil];  // Check your nib name here, seems to be a mistake
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
    [self presentModalViewController:navController animated:YES];
    [navController release];
    [nextWindow release];

【讨论】:

    【解决方案4】:

    需要使用alert view的委托方法

    #pragma mark - AlertView Delegates
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(alertView.tag == 1)
            {
                NoConnection *noConn = [[NoConnection alloc] initWithNibName:@"NoConnecton" bundle:nil];
                [self presentModalViewController:noConn animated:NO];
                [NoConnection release];
            }
    }
    

    别忘了将 alertView 的标签值赋值为 1。

    别忘了遵守UIAlertViewDelegate协议

    快乐编码:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多