委托方法“clickedButtonAtIndex:”不被称为

发布于 2024-08-27 03:21:05 字数 540 浏览 4 评论 0原文

我使用以下代码创建了一个带有两个按钮的警报视图:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: title 
message: msg delegate:nil cancelButtonTitle:@"Replay" otherButtonTitles:@"Highscore", nil];
[alertView show];

我想在单击其中一个按钮时运行一些代码。 为此,我在 delegate.m 文件中添加了以下方法:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex==0) //Run some code 
 else //Other code
 }

但是当我按下任一按钮时,不会调用此方法! 有人能告诉我为什么吗?

预先感谢,

Sagiftw

I have created an alert view with two buttons using the following code:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: title 
message: msg delegate:nil cancelButtonTitle:@"Replay" otherButtonTitles:@"Highscore", nil];
[alertView show];

I want to run some code when one of the buttons is clicked.
In order to do so, I have added the following method to the delegate.m file:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex==0) //Run some code 
 else //Other code
 }

But this method is not called when I press either of the buttons!
Can someone tell me why?

Thanks in advance,

Sagiftw

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酷遇一生 2024-09-03 03:21:05
delegate:nil

如果您指定没有委托,警报视图将如何关联委托?替换该部分

delegate:self

delegate:nil

how will the alert view associate a delegate if you specified there will be no delegates? Replace that part with

delegate:self

instead.

你另情深 2024-09-03 03:21:05

尝试将委托设置为 self 而不是 nil。

Try setting the delegate to self instead of nil.

相思故 2024-09-03 03:21:05

我从 UITextField 委托方法调用 UIAlertView 的 dismissWithClickedButtonIndex:animated: 方法,因为我也想处理键盘返回键:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 1001) {
        [textField resignFirstResponder];
        [_alert dismissWithClickedButtonIndex:1 animated:YES];
    }
    return YES;
}

这样方法 alertView:clickedButtonAtIndex: 甚至永远不会被调用如果您设置了适当的代表。相反,alertView:didDismissWithButtonIndex: 确实被调用。因此,请实现此方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // your code here
}

如果您只想处理警报视图的按钮,则首先调用 clickedButtonAtIndex,然后调用 didDismissWithButtonIndex。

I was calling UIAlertView's dismissWithClickedButtonIndex:animated: method from UITextField delegate method because I wanted to handle the keyboard return key as well:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 1001) {
        [textField resignFirstResponder];
        [_alert dismissWithClickedButtonIndex:1 animated:YES];
    }
    return YES;
}

This way method alertView:clickedButtonAtIndex: never gets called even if you set up proper delegate. Instead alertView:didDismissWithButtonIndex: does get called. So implement this method instead:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // your code here
}

If you just want to handle alert view's buttons only, those first call clickedButtonAtIndex and then didDismissWithButtonIndex.

困倦 2024-09-03 03:21:05

在 .h 中放置 UIActionSheetDelegate 并在 .m 中创建 AlertView 时将委托设置为 self,而不是像上述情况那样设置为 nil

in .h put UIActionSheetDelegate and in .m while creating a alertView put the delegate to self not nil as being done in the above case

忆依然 2024-09-03 03:21:05

这个问题的正确答案是delegate:nil。但是,如果委托已设置为 self,并且 clickedButtonAtIndex 仍然不起作用,请尝试检查是否弹出另一个视图控制器 ([self.navigationController popViewControllerAnimated:YES];< /code>) 显示警报视图后。这也可能导致 clickedButtonAtIndex 未被调用。这就是发生在我身上的事情。

希望这对某人有帮助。

The correct answer for this question is delegate:nil. But in case that the delegate is already set to self, and the clickedButtonAtIndex is still not working, try checking if you are popping another view controller ([self.navigationController popViewControllerAnimated:YES];) after showing the alertview. This can also result to clickedButtonAtIndex not being called. This is what happened to me.

Hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文