1

アプリの起動時にアラート ビューを約 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];
    }
}
4

4 に答える 4

3

メインスレッドでスリープを使用しないでください。これまで。また、バックグラウンド スレッドから UI を更新しないでください。

あなたがしたいことは、メインスレッドにアラートを表示して戻ることです。

次に、ネットワーク コードが完了したら、メイン スレッドにメッセージを送信します。メイン スレッドで、完了の通知を受け取ったら、アラートを削除します。

于 2014-01-13T16:00:45.440 に答える