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 回答