0

カスタムビューをロードするView Controllerがあります(UI要素を描画し、スレッドを生成してバックグラウンドでいくつかのことを行います.

「バックグラウンドで実行されるもの」でエラーが発生した場合、ビューコントローラーがそれをキャッチします。この時点で、bgcolor などの UI 要素を変更したり、新しいラベルを追加したりしたいと考えています。

しかし、私が行った変更は表示されません。これは私がしようとしているものです:

[self performSelectorOnMainThread:@selector(onCompleteFail) withObject:nil waitUntilDone:YES];

- (void)onCompleteFail
{ 

  NSLog(@"Error: Device Init Failed");

  mLiveViewerView.backgroundColor= [UIColor whiteColor];
  //self.view.backgroundColor = [UIColor whiteColor];
  UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];
  tmpLabel.text = @"Failed to init";
  [self.view addSubview:tmpLabel];
}
4

1 に答える 1

1

メイン スレッドで UI 関連の呼び出しを行う必要があります。それは何かを切り替えるのと同じくらい簡単かもしれません

[self onCompleteFail];

[self performSelectorOnMainThread:@selector(onCompleteFail) withObject:nil waitUntilDone:NO];

…または、-onCompleteFail他の理由でバックグラウンド スレッドで呼び出す必要がある場合は、次のように、UI 呼び出しをメイン キューへのディスパッチでラップできます。

- (void)onCompleteFail
{ 
    NSLog(@"Error: Device Init Failed");
    dispatch_async(dispatch_get_main_queue(), ^{
          mLiveViewerView.backgroundColor= [UIColor whiteColor];
          //self.view.backgroundColor = [UIColor whiteColor];
          UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];
          tmpLabel.text = @"Failed to init";
          [self.view addSubview:tmpLabel];
    });
}
于 2013-11-06T03:32:35.520 に答える