0

在我的 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].

如果我将委托设置为nil或自我,它可以正常工作,但是当我按下警报上的按钮时我想打电话alertView:alertView clickedButtonAtIndex:buttonIndex,这不适用于委托设置为 nil 或自我。只有ViewController实现了这些方法,为什么应该是self.delegate,还是?

我希望模型调用警报,但我希望 ViewController 处理警报方法,例如alertView:alertView clickedButtonAtIndex:buttonIndex.

如何才能做到这一点?

4

1 回答 1

0

应该是自己[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
于 2014-06-23T15:23:14.373 回答