objective-c UIAlert按钮不起作用

时间:2013-07-16 01:55:59

标签: iphone ios objective-c uialertview

我是objective-c的新手,我有一个像这样的函数内的UIAlert:

- (void)loadJSON
{

    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"No Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
        });
    } else {
      //some code
    }
}

当用户单击“确定”按钮时,此功能应该启动以调用loadJSON函数? (至少我是这么认为的)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self loadJSON];
}

我的最终目标是,如果没有互联网连接,显示UIAlert,用户看到消息,点击没关系,如果仍然没有互联网连接,则显示警报。我与互联网断开连接,警报信息只出现一次。

我的代码有问题吗?

1 个答案:

答案 0 :(得分:4)

未为alertView分配代理。

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" 
                                                    message: @"No Connection" 
                                                   delegate: self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil]; 
  [alert show];
  [alert release];