我希望当我的应用程序启动时它显示大约 5 秒的警报 View ,然后根据后台进程的结果,它将显示另一个警报 View 。
我遇到的问题是,当我尝试使用 Sleep 来等待后台进程发生时。第一个警报不显示并等待 5 秒。该应用程序显示应用程序的第一个 View ,然后在 5 秒后短暂显示第一个警报。
我需要做什么来执行我想要的。
这是我的代码。
- (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。
您要做的是在主线程上显示警报并返回。
然后,当您的网络代码完成时,让它向主线程发送一条消息。在主线程中,当您收到通知已完成时,请删除警报。
关于ios - 在加载时显示等待警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21095266/