插入警报视图但不起作用

编程入门 行业动态 更新时间:2024-10-27 04:32:50
本文介绍了插入警报视图但不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到一种情况,我需要提醒用户下一个访问的视图控制器是数据加载".

I have a situation where i need to alert users that the next view controller accessed is "Data Loading".

我将此添加到FirstViewController按钮操作中:

I added this to the FirstViewController button action:

- (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之后弹出.

it doesn't work. It loads to SecondViewController and only pops up after the SecondViewController is loaded.

所以我尝试了SecondViewController本身. SecondViewController从远程服务器提取数据,这就是它需要花一些时间才能下载的原因,具体取决于Internet连接.所以我决定在函数中添加UIAlertView:

So i tried on the SecondViewController itself. The SecondViewController extracts data from a remote server which is the reason its going to take a while to download depending on Internet connectivity. So i decided to add the UIAlertView in the function:

- (NSMutableArray*)qBlock{ UIAlertView *alert_initial = [[UIAlertView alloc]initWithTitle:@"Loading" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert_initial show]; NSURL *url = [NSURL URLWithString:@"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; }

这也不起作用.最重要的是,我试图断开互联网连接,以查看是否弹出第二个警报以提醒用户没有互联网连接.第二个警报也不起作用.

This doesn't work too. And to top it off, i tried to off internet connection to see if the second alert pops up to alert user that there's no internet connection. The second alert doesn't work too.

推荐答案

对于问题的第一部分:UIAlertView的show方法不会阻止当前线程,因此执行将继续,并且您将获得行为是期待.您要做的是实现 UIAlertViewDelegate 的方法,并将警报的delegate属性设置为self.因此,解除警报后,您可以显示SecondViewController.

For the first part of the question: the show method of UIAlertView does not blocks the current thread, so the execution continues and the behavior you have is expected. What you have to do is implement one of the UIAlertViewDelegate's methods and set the alert's delegate property to self. So when the alert is dismissed, you can show your SecondViewController.

对于第二部分,如果要在后台线程中执行qBlock方法,则通常会提醒您不要再次显示该警报-您需要在运行UI的主线程中显示警报.为此,请使用以下命令更改您的else语句:

For the second part if you're having your qBlock method executed in a background thread then it's normal you alert not to be shown again - you need to show your alert in the main thread, where the UI is running. To do that change your else statement with the following:

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]; }); }

希望这会有所帮助.

Hope this helps.

更多推荐

插入警报视图但不起作用

本文发布于:2023-11-27 20:30:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   警报   但不

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!