3

我在处理所有服务器请求的方法中实现了可达性功能。我可以通过 NSLogs 看到该功能运行良好。但是,该方法中从来没有“暂停”,这意味着我不能使用 UIAlertView 而不会使程序崩溃。

我可能会以完全错误的方式进行此操作,但我找不到其他任何东西...

有人知道如何以某种方式显示通知吗?

提前致谢

代码:

-(id) getJson:(NSString *)stringurl{
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];

NSLog(@"reached %d", reach.isReachable);

if (reach.isReachable == NO) {

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Passwords don't match."
     message:@"The passwords did not match. Please try again."
     delegate:nil
     cancelButtonTitle:@"OK"
     otherButtonTitles:nil];
     [alert show];

}else{
    id x =[self getJsonFromHttp:stringurl];
    return x;
}
return nil;
}
4

1 回答 1

2

将讨论移至聊天后,我们发现您的 UIAlertView 正在从后台线程调用。永远不要在后台线程中做任何与更新 UI(用户界面)相关的事情。UIAlertView 通过添加一个小弹出对话框来更新 UI,因此它应该在主线程上完成。通过进行以下更改进行修复:

// (1) Create a new method in your .m/.h and move your UIAlertView code to it
-(void)showMyAlert{ 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Passwords don't match." 
                           message:@"The passwords did not match. Please try again." 
                           delegate:nil 
                               cancelButtonTitle:@"OK" 
                           otherButtonTitles:nil]; 
    [alert show]; 

}

// (2) In -(id)getJson replace your original UI-related code with a call to your new method
[self performSelectorOnMainThread:@selector(showMyAlert)
                             withObject:nil
                          waitUntilDone:YES];
于 2013-01-02T19:35:57.550 回答