ios - 在加载时显示等待警报 View

标签 ios objective-c alert wait cocoaasyncsocket

我希望当我的应用程序启动时它显示大约 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/

相关文章:

angular - ionic 2 : Why does alert box look like old Android?

javascript浏览器超时第二个计时器

javascript - 在 iOS 中使用 JSContextGroupSetExecutionTimeLimit

ios - 我必须实现 UIDataSourceModelAssociation 吗?

ios - 复制 UITableViewCell

iOS 6.1 及更高版本使用 ARC 有泄漏

ios - 在phonegap 3.4(cordova)中,notification.alert或window.alert不会直接调用。 (iOS)

android - 处理每个用户的时区和 DST 的任务调度(运行备份)

ios - 我们可以在 iOS 中将数据从 plist 设置到 Keychain 吗?

ios - 如何在 Iphone 上设置不透明度动画?