【问题标题】:Set delegate:self.delegate in an alertView where self.delegate implements UIAlertViewDelegate protocol在 self.delegate 实现 UIAlertViewDelegate 协议的 alertView 中设置 delegate:self.delegate
【发布时间】: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


    【解决方案1】:

    应该是自己[self willPresentAlertView:alert]

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid operation"
                message:@"Cannot divide by 0!"
                delegate:self //self
                cancelButtonTitle:@"Cancel"
                otherButtonTitles:@"Undo division",nil];
    [self willPresentAlertView:alert];//self again
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 2012-03-08
      • 2015-06-14
      • 1970-01-01
      相关资源
      最近更新 更多