初投稿 - お手柔らかにお願いします。
リモート コントロール アプリケーションを 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;
// }
}