我遇到一种情况,我需要提醒用户下一个访问的 View Controller 是“数据加载”。
我将其添加到 FirstViewController 按钮操作中:
- (IBAction)showCurl:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Please Wait" message:@"Acquiring data from server" delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil];
[alert show];
SecondViewController *sampleView = [[SecondViewController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
这不起作用。它加载到 SecondViewController 并仅在 SecondViewController 加载后弹出。
所以我尝试了 SecondViewController 本身。 SecondViewController 从远程服务器提取数据,这就是根据互联网连接需要一段时间才能下载的原因。所以我决定在函数中添加 UIAlertView:
- (NSMutableArray*)qBlock{
UIAlertView *alert_initial = [[UIAlertView alloc]initWithTitle:@"Loading" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert_initial show];
NSURL *url = [NSURL URLWithString:@"http://www.somelink.php"];
NSError *error;
NSStringEncoding encoding;
NSString *response = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
if (response) {
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSMutableArray *sample = [responseString JSONValue];
return sample;
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ALERT" message:@"Internet Connection cannot be established." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
return NULL;
}
这也行不通。最重要的是,我尝试关闭互联网连接,看看是否会弹出第二个警报来提醒用户没有互联网连接。第二个警报也不起作用。
最佳答案
对于问题的第一部分:UIAlertView
的 show
方法不会阻塞当前线程,因此执行会继续,并且您的行为是预期的。您所要做的就是实现 UIAlertViewDelegate 之一的方法并将警报的 delegate
属性设置为 self
。因此,当警报解除时,您可以显示您的 SecondViewController
。
对于第二部分,如果您在后台线程中执行 qBlock
方法,那么您警报不再显示是正常的 - 您需要在主线程中显示警报,其中 UI 是运行。为此,请使用以下内容更改 else
语句:
else
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ALERT" message:@"Internet Connection cannot be established." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});
}
希望这有帮助。
关于objective-c - 插入警报 View 但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10430606/