单击按钮前警报崩溃 EXC_BAD_ACCESS

Alert crash before clicking the button EXC_BAD_ACCESS

我需要发出警告警报,它将在点击之前执行操作 "OK",默认的 UIAlert 在点击之前崩溃,没有任何错误,所以我尝试使用 SIAlert,但它也崩溃了,错误是 EXC_BAD_ACCESS...如何解决?请帮帮我? Xcode6.1.1

- (IBAction)share:(id)sender {

SIAlertView *alertView = [[SIAlertView alloc] initWithTitle:@"Post to VK wall?" andMessage:@"Do u want 2 post VK?"];


[alertView addButtonWithTitle:@"OK"
                         type:SIAlertViewButtonTypeCancel
                      handler:^(SIAlertView *alertView) {
                          NSLog(@"Button3 Clicked");
                      }];
[alertView addButtonWithTitle:@"CANCEL"
                         type:SIAlertViewButtonTypeDestructive
                      handler:^(SIAlertView *alertView) {
                          NSLog(@"Button2 Clicked");
                      }];

alertView.willShowHandler = ^(SIAlertView *alertView) {
    NSLog(@"%@, willShowHandler", alertView);
};
alertView.didShowHandler = ^(SIAlertView *alertView) {
    NSLog(@"%@, didShowHandler", alertView);
};
alertView.willDismissHandler = ^(SIAlertView *alertView) {
    NSLog(@"%@, willDismissHandler", alertView);
};
alertView.didDismissHandler = ^(SIAlertView *alertView) {
    NSLog(@"%@, didDismissHandler", alertView);
};

[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

alertView.title = @"Postin VK";}

http://i.stack.imgur.com/kwn9Y.png - 图片错误

替换为:

[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

有了这个:

[alertView show];

我认为它没有找到@selector(show)

alertView 在方法结束时被释放,这就是你得到异常的原因。

你应该在你的 class 中保留对它的强烈引用。像这样:

@implementation MyClass {
    SIAlertView *_alertView;
}

- (IBAction)share:(id)sender {
    _alertView = [[SIAlertView alloc] initWithTitle:@"Post to VK wall?" andMessage:@"Do u want 2 post VK?"];
    ...
}

@end