0

接続を待っているアプリケーションがあります。アプリケーションが待機している間、プログラムによって、またはユーザーが AlertView のキャンセル ボタンをクリックすることによって、AlertView をユーザーに表示する必要があります。

メインスレッドはクライアントが接続するのを待っています。別のスレッドのように、ダイアログの時間を更新している AlertView を作成して表示しています。スレッドには、AlertView のテキストを更新する has NSRunLoop があります。

AlertView がタッチ イベントを受信せず、プログラムで却下されないことを除いて、すべて正常に動作します。ここで私が間違っているかもしれないことに誰かが光を当てることができますか.

これがサンプルコードです。

funcA() {

    [NSThread detachNewThreadSelector:@selector(createDialog) toTarget:self withObject:nil];

    [NSThread detachNewThreadSelector:@selector(updateDialog) toTarget:self withObject:nil];

    ..

    ..

    BlockingWait();

    ..

    ..

}

- (void) createDialog {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];

    ...

    label = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 20.0f, 225.0f, 90.f)];

    [alert show];

    [alert release];

    [pool drain];

}

- (void) upDateDialog {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSRunLoop *loop = [NSRunLoop currentRunLoop];

    timer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateText) userInfo:nil repeats:YES];

    [loop run];

    [pool drain];
}

- (void) updateText {

    label.text = " Wait for" + n +  "secs";

    n--;

    if ( n == 0 )
      [alert dismissWithClickedButtonIndex:0 animated:YES];  // Doesn't work
}


- (void) alertView: (UIAlertView *) alert clickedButtonAtIndex:(NSInteger) buttonIndex {
// Never Gets called
    NSLog(@"Alert is dissmissed with button index %d", buttonIndex);

    if (buttonIndex == 0) {

        [timer invalidate];
    }
}
4

2 に答える 2

2

私はあなたのコードさえ読んでいませんでした、ただ見出しはあなたが本当にあなたのアーキテクチャに取り組むべきであることをすでに示しています。UIKitをハックすることはできますが、UIスレッドによってのみ管理されるように設計されています。したがって、ブロッキング呼び出しをスレッドに移動し、メインスレッドからUIAlertViewを管理します。

于 2012-01-16T15:46:11.240 に答える
0

メインスレッドをブロックしないでください。決して。を非同期にクレートUIAlertViewし、他の作業を行っている間は ivar に保持し、必要に応じて更新します。これにスレッドやブロッキングが必要になることはほとんどありません。NSURLConnectionブロックせずに接続を管理するには、非同期で使用するだけです。

于 2012-01-16T15:50:10.277 に答える