質問

特定のボタンが押されたときに

私のアプリでは、私は別のスレッド上のWebサービスを解析する方法(postButtonClicked:)を呼び出します。私はその後、コールが成功したかどうかを通知するようにユーザーにUIAlertViewを表示します。

ここで私が使用するコードがあります:

- (void)postButtonClicked:(id)sender {
    [NSThread detachNewThreadSelector:@selector(postViaWebservices) toTarget:self withObject:nil];
}

- (void)postViaWebservices {

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

    WebServiceManager *wsm = [[WebServiceManager alloc] init];

    BOOL success = [wsm callPost];

    if (success) {
        [self performSelectorOnMainThread:@selector(postSuccess) withObject:nil waitUntilDone:NO];
    } else {
        [self performSelectorOnMainThread:@selector(postFailure) withObject:nil waitUntilDone:NO];
    }   

    [wsm release];

    [pool release];

}

- (void)postSuccess {

    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:nil
                          message:@"Success message"
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];

    [alert show];
    [alert release];

}

- (void)postFailure {

    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:nil
                          message:@"Failure message"
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];

    [alert show];
    [alert release];

}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    ...

}
私は(私は表示もうalertView:clickedButtonAtIndex:ために必要な)ビューコントローラにUIAlertViewを添加UNTIL

これは、すべてうまく働きました。今、私はpostButtonClicked:呼び出すたびに、アプリケーションがクラッシュします。しかし、私はalertView:clickedButtonAtIndex:を削除する場合は、postButtonClicked:を呼び出し、アプリケーションがOKに動作します。

私はこの問題を解決するために何をする必要があるかわからない。

役に立ちましたか?

解決

にかかわらず、アラートビューのデリゲートメソッドの内容は/メッセージを失敗し、成功に関連しているかどうか、それはまだそのための方法の内容はあなたの答えを得るための唯一の方法かもしれたちを示し、問題になる可能性の。あなたが/成功の上に[OK]をタップし、メッセージを失敗したときにselfとしてアラートのデリゲートを設定しているので、いずれかの方法では、アラートビューのデリゲートメソッドが呼び出されます。あなたはこの方法が成功を却下する場合/警告を失敗起動したくない場合は、デリゲートを設定しないでください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top