-2

我已经实现了这样的警报:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time Over"
                                                    message:@"Player Two won!"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alert show];

现在我想设置score = 0按下“确定”的时间。有人可以帮助我吗?

4

2 回答 2

0

您必须alertView:willDismissWithButtonIndex:为按钮点击添加委托方法。

当一个按钮被点击时,它将被调用,并且buttonIndex将是被点击的按钮的索引(显然)。

但是,如果您只有一个按钮,则实际上并不需要索引(除非您计划稍后将其他按钮添加到视图中)。

这应该可以解决问题:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger) buttonIndex {
    score = 0;
}

为此,您必须将delegate参数从更改nilself


正如Aaron Brager 所 指出的,如果您只针对 iOS 8,您可能希望使用UIAlertController,因为UIAlertView已弃用:

UIAlertController *alert = [UIAlertController
                                    alertControllerWithTitle:@"Time Over"
                                    message:@"Player Two won!"
                                    preferredStyle:UIAlertControllerStyleAlert];

然后,要添加一个动作,只需执行以下操作:

UIAlertAction *dismiss = [UIAlertAction
                     actionWithTitle:@"OK"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction *action) {
                         score = 0; 
                     }];
[alert addAction:dismiss];

最后,呈现观点:

[self presentViewController:alert animated:YES completion:nil];
于 2015-03-13T01:48:53.517 回答
0

(如果您想在警报上使用多个按钮)在此处引用 UIAlertViewDelegate 方法AlertViewDelegate Reference并将您的 UIAlertView 实例设置为这样的委托alert.delegate = self;

于 2015-03-13T01:49:33.863 回答