- (void)alertViewCancel:(UIAlertView *)alertView 未被调用
我遇到的问题是,当我使用取消按钮取消 AlertView 时,不会调用 UIAlertViewDelegate 方法 - (void)alertViewCancel:(UIAlertView *)alertView 。
奇怪的是委托方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 工作得很好。
有人有想法吗?
提前致谢
肖恩
- (void)alertViewCancel:(UIAlertView *)alertView
{
if(![self aBooleanMethod])
{
exit(0);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
}
我在单击按钮时称之为:
- (void)ImagePickDone
{
UIAlertView *alertDone = [[UIAlertView alloc]
initWithTitle:@"Done"
message:@"Are u sure?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles: @"Yes", nil];
[alertDone show];
[alertDone release];
}
I've got the problem that the UIAlertViewDelegate method - (void)alertViewCancel:(UIAlertView *)alertView is not called when I cancel a AlertView with it's cancel button.
Weird is that the delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works perfectly.
Does anyone have an idea?
Thanks in advance
Sean
- (void)alertViewCancel:(UIAlertView *)alertView
{
if(![self aBooleanMethod])
{
exit(0);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
}
I call this when a button is clicked:
- (void)ImagePickDone
{
UIAlertView *alertDone = [[UIAlertView alloc]
initWithTitle:@"Done"
message:@"Are u sure?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles: @"Yes", nil];
[alertDone show];
[alertDone release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AlertViewCancel 用于系统关闭警报视图时,而不是当用户按下“取消”按钮时。引用自苹果文档:
如果您想捕获用户按下“取消”按钮的时间,您应该使用 clickedButtonAtIndex 方法并检查索引是否与取消按钮的索引相对应。要获取该索引,请使用:
The alertViewCancel is used for when the system dismisses your alert view, not when the user presses the "Cancel" button. Quote from apple docs:
If you want to capture when the user presses the "Cancel" button you should use the clickedButtonAtIndex method and check that the index corresponds to the index for the cancel button. To obtain this index use:
您可以在此委托的索引 0 处处理取消:
You can handle the Cancel at the index 0 of this delegate:
这可以通过两种方式改进。首先,它只处理用户实际单击按钮的情况。它不处理调用 [myAlertmissWithClickedButtonIndex:] 的情况,或者以其他方式解除警报的情况。其次,按钮 0 不一定是取消按钮。在具有两个按钮的警报中,左侧按钮位于索引 0,右侧按钮位于索引 1。如果您更改了标题以使右侧按钮显示“取消”,则按钮 1 逻辑上就是“取消”按钮。您可以实现“didDismiss”,而不是“willDismiss”,它将在对话框消失之后而不是之前调用。
This can be improved in two ways. First, it only handles the case that the user actually clicked a button. It doesn't handle the situation that [myAlert dismissWithClickedButtonIndex:] is called, or that the alert is dismissed in some other way. Second, button 0 is not necessarily the cancel button. In an alert with two buttons, the left one is at index 0, and the right one is at index 1. If you changed the titles so that the right button says "Cancel", then button 1 is logically the Cancel button. Instead of "willDismiss" you can implement "didDismiss" which will be called after the dialog has disappeared and not before.