【问题标题】:UIAlertView exception in reachability可达性中的 UIAlertView 异常
【发布时间】:2012-07-18 13:44:58
【问题描述】:

我是 iOS 新手,在以编程方式关闭 UIAlertView 时遇到问题。

我在 .h 文件中声明了警报:

@interface ViewController : UIViewController<UIWebViewDelegate>{
    UIWebView *webView;
    UIAlertView *alert;
}

@property(nonatomic, retain) IBOutlet UIWebView *webView;
@property(nonatomic, retain) IBOutlet UIAlertView *alert;

在 .m 文件中,我使用此警报来警告用户有关互联网连接:

@synthesize alert;
-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
    case NotReachable:
    {
        NSLog(@"The internet is down.");
        self.internetActive = NO;
        alert = [[[UIAlertView alloc] initWithTitle:@"Network Unavailable" 
                                                         message:@"Internet connectivity could not be established!" 
                                                        delegate:self 
                                               cancelButtonTitle:nil 
                                               otherButtonTitles:nil] autorelease];
        [alert show];
        break;
    }
    default:
    {
        [alert dismissWithClickedButtonIndex:0 animated:NO];

    }
}

}

现在,当互联网连接中断时,会出现警报。但是,当 Internet 连接建立时,程序会抛出异常,抱怨对对象的访问不正确。谁能告诉我我做错了什么?

【问题讨论】:

    标签: iphone ios ios5 uialertview reachability


    【解决方案1】:

    1- 在头文件中实现 UIAlertViewDelegate

    2-使用这个方法

     -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
    }
    

    在此方法中,根据单击按钮的索引关闭警报视图

    【讨论】:

      【解决方案2】:
      [alert dismissWithClickedButtonIndex:0 animated:NO];  
      

      在所有其他情况下都被调用,并且在调用该方法时还没有为 alert 分配任何内存。也没有必要添加那条线。 或者你可以像这样修改你的代码。

      1. 保留对 Alert 的引用(虽然这是一个非常糟糕的主意)
      2. 在切换条件下检查是否显示了警报框,然后调用dismiss并释放警报视图

      【讨论】:

        【解决方案3】:

        这应该可行:

            default:
            {
                if (alert){
                    [alert dismissWithClickedButtonIndex:0 animated:NO];
                }
            }
        

        在你显示之前假设“alert”确实是“nil”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-08
          • 2014-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-01
          • 1970-01-01
          • 2013-02-12
          相关资源
          最近更新 更多