UIAlertView委托崩溃应用程序

时间:2013-03-11 19:57:14

标签: ios xcode

我通过以下方式将委托设置为UIAlertView:

PhotoViewController *controller = [[PhotoViewController alloc] init];

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: @"Announcement"
                      message: @"It turns out that you are playing Addicus!"
                      delegate: controller
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
[alert show];

PhotoViewController不是当前的UIviewContorller。警报加载正常,但是当我按下确定按钮时,应用程序崩溃并且我收到此错误:

0x3a61a5b0:ldr r3,[r4,#8] EXC BAD ACCESS

这是什么意思?

编辑:

+ (DejalActivityView *)activityViewForView:(UIView *)addToView withLabel:(NSString *)labelText width:(NSUInteger)aLabelWidth;
{
// Immediately remove any existing activity view:
if (dejalActivityView)
    [self removeView];

// Remember the new view (so this is a singleton):
dejalActivityView = [[self alloc] initForView:addToView withLabel:labelText width:aLabelWidth];

if ([labelText isEqualToString:@"Uploading photo (this process might take a while)"]) {
buttonCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonCancel addTarget:dejalActivityView
                 action:@selector(callCancelAlert:)
       forControlEvents:UIControlEventTouchDown];
buttonCancel.frame = CGRectMake(230, 540, 265, 40);
[addToView addSubview:buttonCancel];
[buttonCancel setImage:[UIImage imageNamed:@"socialize-navbar-bg.png"] forState:UIControlStateNormal];
[buttonCancel setTitle:@"Cancel upload" forState:UIControlStateNormal];
}

return dejalActivityView;
}

-(IBAction)callCancelAlert:(id)sender{

PhotoViewController *controller = [[PhotoViewController alloc] init];

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: @"Announcement"
                      message: @"It turns out that you are playing Addicus!"
                      delegate: controller
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
[alert show];
//[alert release];
}

1 个答案:

答案 0 :(得分:4)

代表不是保留属性。这意味着当controller超出范围时,ARC将释放它,并且由于它未被另一个对象保留,因此将取消分配。当警报视图尝试向其委托发送消息时,该地址不再有效,并且您将获得异常。

警报视图的委托需要比警报视图本身更长寿。通常,创建警报视图的对象应该是委托。您应该将委托代码移动到创建警报视图的类中。