我是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,用户看到消息,单击确定,如果仍然没有互联网连接,则显示警报。我已断开与互联网的连接,警报消息仅出现一次。

我的代码有问题吗?

最佳答案

没有为AlertView分配委托。

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

关于iphone - Objective-C的UIAlert按钮不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17666795/

10-14 21:42
查看更多