0

初投稿 - お手柔らかにお願いします。

リモート コントロール アプリケーションを Android から iPhone に移植しようとしていますが、メイン ビューのメソッドで dispatch_async を使用したスレッド内通信に問題があります。

アプリケーションには、リモート クライアントとの通信を処理する別の ControlPanel クラスがあります。ControlPanel 通信オブジェクトは、デバイスへの継続的なネットワーク通信を処理するバックグラウンド スレッドとしてインスタンス化されます。単純なテキスト文字列を UI スレッドに渡す (テキスト フィールドを更新する) には、バックグラウンド スレッドが必要です。

私は多くの例を見てきましたが、定義を扱っているものはないようで、これについてぐるぐる回っています。ControlPanel クラスでメソッドを定義するにはどうすればよいですか? 私はさまざまな定義方法を試しましたが、前進していません。簡単なものがありませんか?これを行う方法として、viewDidLoad の updateKeypadDisplay が提案されましたが、どこが間違っているかを確認するのに十分な構文を理解していません。

/*  
 *   In the ControlPanel.m class the thread does stuff and wants to update the UI:
 */

NSString * kpString = @"String Updated By Process";

dispatch_async(dispatch_get_main_queue(), ^{
    self.updateKeypadDisplay(kpString);    // << This call gives an error - property not found on object of type ControlPanel                          
});


/*  
 *   In the ControlPanel.h header :
 */
- (void)updateKeypadDisplay:(void (^)(NSString *kpString))block;




/*  
 *   In the ViewController .m
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    kpdisplay.text = @"Initial String";
    backgroundQueue = dispatch_queue_create("com.sss.tt.bgqueue", NULL);  

    dispatch_async(backgroundQueue, ^{
        self.panelobj = [[ControlPanel alloc ]init];
        [self.panelobj connectPanel];
    });

    //   This was suggested as the way to define the method to update the main UI kpdisplay text field, but
    //    I cannot get the syntax right.   I'm also unsure the definitions are correct in the headers.
    //
    //       [ControlPanel updateKeypadDisplay::^(NSData *kpString) {
    //       self.kpdisplay.text = kpString;
    //       }

}
4

1 に答える 1

0

バックグラウンド スレッドから UI を更新する場合は、メイン スレッド (すべての UI が処理される場所) にディスパッチ async を呼び出す必要があります。

dispatch_async(dispatch_get_main_queue(), ^(void){
    NSLog(@"I can update UI elements here!");
});
于 2014-03-23T20:55:03.010 に答える