我在处理所有服务器请求的方法中实现了Reachability功能。我可以通过NSLog看到该功能完美运行。但是,方法中永远不会出现“暂停”,这意味着我不能在不崩溃程序的情况下使用UIAlertView。
我可能会以完全错误的方式解决这个问题,但我找不到任何其他的东西......
是否有人知道如何以某种方式显示通知?
提前致谢
CODE:
-(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;
}
答案 0 :(得分: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];