在加载时显示等待的警报视图 [英] Showing Waiting Alert View At Load
问题描述
我希望在我的应用启动时显示警报视图约5秒钟,然后根据后台进程的结果,它将显示另一个警报视图.
I would like when my app starts for it to show an Alert view for about 5 seconds, then depending on the outcome of a background process, it will show another Alert view.
我遇到的问题是,当我尝试使用睡眠来等待后台进程发生时.第一个警报不显示,请等待5秒钟.该应用程序显示该应用程序的第一个视图,然后在5秒钟后短暂显示第一个警报.
The problem I am experiencing is that when I try to use Sleep to wait for a background process to occur. The first Alert does not show and wait the 5 seconds. The app shows the first view of the app, and then after 5 seconds the first Alert shows briefly.
要执行自己想要的事情,我需要做些什么.
What do I need to do to perform what I wish.
这是我的代码.
- (void)viewDidAppear:(BOOL)animated
{
SSGlobalSettings *connSettings = [SSGlobalSettings sharedManager];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connecting" message:@"Please wait, while your device connects" delegate:Nil cancelButtonTitle:nil otherButtonTitles: nil];
[alertView show];
[NSThread sleepForTimeInterval:5.0f];
[alertView dismissWithClickedButtonIndex: alertView.cancelButtonIndex animated: YES];
if ([connSettings.connectionStatus isEqual: @"Not Found"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"Cannot find your device on the network" delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connection Success" message:@"WYour device has been found on the network" delegate:@"OK" cancelButtonTitle:nil otherButtonTitles: nil];
[alertView show];
}
}
推荐答案
不要在主线程上使用sleep.曾经.也不要从后台线程更新UI.
Don't use sleep on the main thread. Ever. Also don't update UI from a background thread.
您要做的是在主线程上显示警报并返回.
What you want to do is to display your alert on the main thread and return.
然后,当您的网络代码完成时,让它向主线程发送一条消息.在主线程中,当您收到已完成的通知时,请删除警报.
Then, when your networking code completes, have it send a message to the main thread. In the main thread, when you receive a notice that it's done, remove the alert.
这篇关于在加载时显示等待的警报视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!