将委托设置为self时,UIAlertview委托将崩溃

时间:2013-02-11 08:27:44

标签: xcode crash uialertview exc-bad-access

我有这段简单的代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact" message:@"This contact does not exist yet" delegate:self cancelButtonTitle:@"Ok"  otherButtonTitles:@"Not now", nil];
[alert show];

如果我将委托设置为'nil',一切都很好。但是,如果我将委托设置为'self'并添加clickedButtonAtIndex或didDismissWithButtonIndex委托,则应用程序将与EXC_BAD_ACCESS崩溃

3 个答案:

答案 0 :(得分:0)

我认为你没有设置alertView委托方法。

首先在.h文件中设置alertView委托协议。

     @interface MainViewController : UIViewController<UIAlertViewDelegate>

然后实现这个方法,它会正常工作

  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
   switch (buttonIndex)
  {
    case 0:

        break;
    case 1:
        break;

    default:
        break;
  }
}

答案 1 :(得分:0)

你的问题是你的对象(自我)不再存在了!但是alertview尝试访问它,所以你得到EXC_BAD_ACCESS。检查你的委托对象(自己)是否还活着!

答案 2 :(得分:0)

问题是由于我的流量。我有一个类调用URL。事实上,Viewcontroller确实在服务器响应之前完成了很久。因此,我必须在调用者中实现NSRunLoop,以便等待服务器通信完成。基于调用例程的一些ExitCode,我只能显示一个警报并让代理处理按下的按钮。无论如何,谢谢Chakalaka让我走上正轨。

相关问题