- (void)alertViewCancel:(UIAlertView *)alertView 未被调用

发布于 2024-08-25 00:51:44 字数 976 浏览 4 评论 0原文

我遇到的问题是,当我使用取消按钮取消 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 技术交流群。

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

发布评论

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

评论(3

热风软妹 2024-09-01 00:51:44

AlertViewCancel 用于系统关闭警报视图时,而不是当用户按下“取消”按钮时。引用自苹果文档

您可以选择实施
AlertViewCancel:采取的方法
当系统采取适当的行动
取消您的警报视图。如果
delegate 没有实现这个
方法,默​​认行为是
模拟用户点击取消
按钮并关闭视图。

如果您想捕获用户按下“取消”按钮的时间,您应该使用 clickedButtonAtIndex 方法并检查索引是否与取消按钮的索引相对应。要获取该索引,请使用:

index = alertDone.cancelButtonIndex;

The alertViewCancel is used for when the system dismisses your alert view, not when the user presses the "Cancel" button. Quote from apple docs:

Optionally, you can implement the
alertViewCancel: method to take the
appropriate action when the system
cancels your alert view. If the
delegate does not implement this
method, the default behavior is to
simulate the user clicking the cancel
button and closing the view.

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:

index = alertDone.cancelButtonIndex;
初见 2024-09-01 00:51:44

您可以在此委托的索引 0 处处理取消:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
      //cancel button clicked. Do something here.
    }
    else{
      //other button indexes clicked
    }
}   

You can handle the Cancel at the index 0 of this delegate:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
      //cancel button clicked. Do something here.
    }
    else{
      //other button indexes clicked
    }
}   
傲影 2024-09-01 00:51:44

这可以通过两种方式改进。首先,它只处理用户实际单击按钮的情况。它不处理调用 [myAlertmissWithClickedButtonIndex:] 的情况,或者以其他方式解除警报的情况。其次,按钮 0 不一定是取消按钮。在具有两个按钮的警报中,左侧按钮位于索引 0,右侧按钮位于索引 1。如果您更改了标题以使右侧按钮显示“取消”,则按钮 1 逻辑上就是“取消”按钮。您可以实现“didDismiss”,而不是“willDismiss”,它将在对话框消失之后而不是之前调用。

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == alertView.cancelButtonIndex)
    {
      //cancel button clicked. Do something here.
    }
    else
    {
      //other button indexes clicked
    }
}   

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.

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == alertView.cancelButtonIndex)
    {
      //cancel button clicked. Do something here.
    }
    else
    {
      //other button indexes clicked
    }
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文