iphone - objective-c UIAlert 按钮不起作用

标签 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
    }
}

当用户单击 OK 按钮时,该函数应该启动以调用 loadJSON 函数吗? (至少我是这么认为的)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self loadJSON];
}

我的最终目标是如果没有互联网连接,则显示 UIAlert,用户看到消息,单击确定,如果仍然没有互联网连接,则显示警报。我断开了与 Internet 的连接,并且警报消息只出现一次。

我的代码有问题吗?

最佳答案

未为 alertView 分配委托(delegate)。

    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/

相关文章:

php - 解析推送已停止

ios - 我什么时候应该将 deleteCacheWithName 与 fetchedResultsController 一起使用?

ios - Swift 中的存在运算符是什么?

objective-c - NSStream : Is there any airtight defense against blocking?

iphone - 在uitextview中显示微笑

iphone - Cocos2d安装

iphone - 无法通过应用程序加载器提交

iOS 应用程序评论 - 视频流 MPEG-DASH 时间超过 10 分钟

c++ - xcode 使用需要 './configure' ,'make' 和 'make install' 的外部库构建项目

objective-c - 核心音频 : is zero equivalent to silence only for PCM audio?