委托方法“clickedButtonAtIndex:”不被称为
我使用以下代码创建了一个带有两个按钮的警报视图:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您指定没有委托,警报视图将如何关联委托?替换该部分
。
how will the alert view associate a delegate if you specified there will be no delegates? Replace that part with
instead.
尝试将委托设置为 self 而不是 nil。
Try setting the delegate to self instead of nil.
我从 UITextField 委托方法调用 UIAlertView 的 dismissWithClickedButtonIndex:animated: 方法,因为我也想处理键盘返回键:
这样方法 alertView:clickedButtonAtIndex: 甚至永远不会被调用如果您设置了适当的代表。相反,alertView:didDismissWithButtonIndex: 确实被调用。因此,请实现此方法:
如果您只想处理警报视图的按钮,则首先调用 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:
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:
If you just want to handle alert view's buttons only, those first call clickedButtonAtIndex and then didDismissWithButtonIndex.
在 .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
这个问题的正确答案是
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 theclickedButtonAtIndex
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 toclickedButtonAtIndex
not being called. This is what happened to me.Hope this helps someone.