【发布时间】:2014-01-13 15:40:06
【问题描述】:
我希望当我的应用启动时,它会显示大约 5 秒的警报视图,然后根据后台进程的结果,它会显示另一个警报视图。
我遇到的问题是,当我尝试使用睡眠等待后台进程发生时。第一个警报不显示并等待 5 秒。该应用程序显示应用程序的第一个视图,然后在 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];
}
}
【问题讨论】:
-
因为您将警报设置为在另一个线程上运行,而不是在
UIAlertView将运行的主线程上。要让主线程休眠,只需调用sleep(5) -
完全相同的结果。我相信 [NSThread sleepForTimeInterval:5.0f] 确实阻塞了同一个 UI 线程。好吧,它在这里声明-stackoverflow.com/questions/2232366/…
-
正如
DuncanC所说,无论如何你都不应该阻塞主线程。我什至不建议使用sleep(5);这就是为什么我没有将其添加为答案。
标签: ios objective-c alert wait cocoaasyncsocket