【发布时间】:2014-06-23 15:18:44
【问题描述】:
在我的 ViewController 中,我实现了 UIAlertViewDelegate,在我的模型中,我有一个方法可以创建一个 UIAlertView,其委托设置为 self.delegate,它是 ViewController 的一个实例。然后我尝试像这样(在模型中)调用 willPresentAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid operation"
message:@"Cannot divide by 0!"
delegate:self.delegate
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Undo division",nil];
[self.delegate willPresentAlertView:alert];
willPresentAlertView 在 ViewController 中是这样实现的:
- (void)willPresentAlertView:(UIAlertView *)alertView
{
[alertView show];
}
然后我在调用[alert show] 时收到错误Thread 1:EXC_BAD_ACCESS (code=2,adress=0xbf7fff6c)。
如果我将委托设置为nil 或self,它可以正常工作,但是当我按下警报上的按钮时,我想调用alertView:alertView clickedButtonAtIndex:buttonIndex,这在委托设置为nil 或self 时不起作用。只有ViewController实现了这些方法,为什么应该是self.delegate,还是?
我希望模型调用警报,但我希望 ViewController 处理警报方法,例如alertView:alertView clickedButtonAtIndex:buttonIndex。
如何做到这一点?
【问题讨论】:
标签: objective-c xcode delegates uialertview uialertviewdelegate