2

私のiPhoneアプリには、NSObjectAクラスとUIViewControllerBクラスがあります。AからBクラスのインスタンスメソッドを呼び出したい。次のコードを使用した。

Bclass *vc = [[Bclass alloc]init];
[vc hideAlert:NSString];
[vc release];

およびBクラスの場合:

- (void)hideAlert:(NSString*)message{
    UIAlertView *shareAlrt = [[UIAlertView alloc] initWithTitle:@""
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [shareAlrt show];
    [shareAlrt release];
}

そして、メソッドが呼び出され、AlertViewが表示されます。[OK]ボタンをクリックすると、クラスCclassに移動します。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        Cclass *vc = [[Cclass alloc]initWithNibName:@"Cclass" bundle:[NSBundle mainBundle]];
        [self presentModalViewController:vc animated:NO];
        [vc release];
    }
}

しかし、[OK]ボタンをクリックすると、アプリがクラッシュします。ここで何が起こっているのですか?B class.hファイルに追加<UIAlertViewDelegate>しましたが、それでも同じエラーが発生します。助けてください

エラーコードが表示されます*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType alertView:clickedButtonAtIndex:]: unrecognized selector sent to instance 0x81baa80'

4

2 に答える 2

0

方法を変えるだけ

- (void)hideAlert:(NSString*)message{
    UIAlertView *shareAlrt = [[UIAlertView alloc] initWithTitle:@""
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"Ok",nil];
    [shareAlrt show];
    [shareAlrt release];
}
于 2012-06-06T11:40:07.317 に答える
-1

これは、「OK」というタイトルのキャンセルボタン以外に他のボタンがないと仮定することで答えられました。仮定は、表示されたコードを見ることによって行われます。

デリゲートを処理できない[キャンセル]ボタンを使用して、アクションを実行しました。

UIAlertViewDelegateクラスリファレンスのドキュメントを見ると

オプションで、alertViewCancel:メソッドを実装して、システムがアラートビューをキャンセルしたときに適切なアクションを実行できます。デリゲートがこのメソッドを実装していない場合、デフォルトの動作は、ユーザーがキャンセルボタンをクリックしてビューを閉じることをシミュレートすることです。

于 2012-06-06T11:18:34.013 に答える