显示等待警报视图在加载

我想当我的应用程序启动它显示一个警报视图约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]; } } 

不要在主线上使用睡眠。 永远。 也不要从后台线程更新UI。

你想要做的就是在主线程上显示你的警报并返回。

然后,当你的networking代码完成后,让它发送消息到主线程。 在主线程中,当您收到通知已完成时,请删除警报。

这是不正确的,因为你试图告诉应用程序的主线睡觉。 如果你要让这个线程进入hibernate状态,那么在这段时间内你很可能不会允许任何UI更新,因为所有的UI更新都是在主线程中进行的。

你需要做的是将显示第二个UIAlertView的代码移动到第二个方法,然后调用方法- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay ,select器的第二种方法,并给它延迟5秒。 确保你删除了呼叫来睡主线程。

但是,这似乎仍然是一个不好的做法,因为这个动作应该是由于你的后台任务完成而发生的。 所以如果可以的话,你应该运行代码来显示完成任务后的第二个警报视图,这很可能会在不同的时间内完成。

你在你的方式阻塞主线程。

我觉得你只是希望用户在第一个5秒之前不要做任何事情(为了让你连接成功?),如果是的话,很多方法可以做到这一点,例如只是在窗口顶部显示一个视图,直到你想要用户可以做的事情,你可以在该视图上显示消失button,或者立即消失。

你可以使用performSelector:withObject:afterDelay:方法。

    Interesting Posts