0

我通过以下方式将委托设置为 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 错误访问

这是什么意思?

编辑:

+ (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];
}
4

1 回答 1

4

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

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

于 2013-03-11T20:23:11.230 回答