0

テキスト入力で警告ウィンドウを表示するはずのこのコード:

self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];

次のエラーが発生します。

Thread 7: Program received signal: "EXC_BAD_ACCESS"

これは self.alert がどのように定義されているかです:

@interface MyClass : NSObject
{
    UIAlertView *alert;
    id <MyClassDelegate> __unsafe_unretained delegate;
}

@property (nonatomic, retain) UIAlertView *alert;
@property (unsafe_unretained) id <MyClassDelegate> delegate;
4

2 に答える 2

1

カスタマイズが原因かもしれません。

理由はわかりませんが、問題はスレッドの使用とアラートのカスタマイズにあるようです。

このアラートをメイン スレッドに表示してみてください。何が起きましたか?

おそらく次の行でエラーが発生します: self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;

はいの場合、メインスレッドでこれを実行する必要があります。

- (void) yourMethod{
    [self performSelectorOnMainThread:@selector(yourMethod2) withObject:nil waitUntilDone:NO];
}

- (void) yourMethod2{
    self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [self.alert show];
}

申し訳ありませんが、それ以上のことはできませんが、何が起こるか正確にはわかりませんが、他のスレッドで表示するものを編集する際の問題については既に読みました。

それがあなたを助けることを願っています!

于 2012-04-06T00:17:25.103 に答える
0

これEXC_BAD_ACCESSは、解放されたオブジェクトへのアクセスが原因です。これを回避するには、UIAlertView一種のモーダルを呼び出します。

関数本体:

-(void)checkSaving
{
    UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"Do you want to add these results to your database?"
        message:@"\n\n"
        delegate:self
        cancelButtonTitle:@"No"
        otherButtonTitles:@"Save", nil];

    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];

    //this prevent the ARC to clean up :
    NSRunLoop *rl = [NSRunLoop currentRunLoop];
    NSDate *d;
    d= (NSDate*)[d init];
    while ([alert isVisible]) 
    {
     [rl runUntilDate:d];

    }
}

あなたの選択結果:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons

    if (buttonIndex == 1)//Save
    {
        //do something

    }
    if (buttonIndex == 0)//NO
    {
        //do something
    }
}

関数をインターフェイス宣言に登録します。

@interface yourViewController ()
    -(void)checkSaving
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
@end

呼び出すには:

[self checkSaving];

これがお役に立てば幸いです。

于 2012-12-08T13:06:43.697 に答える