【问题标题】:- (void)alertViewCancel:(UIAlertView *)alertView is not called- (void)alertViewCancel:(UIAlertView *)alertView 未被调用
【发布时间】:2011-01-27 17:47:32
【问题描述】:

我遇到的问题是 UIAlertViewDelegate 方法 - (void)alertViewCancel:(UIAlertView *)alertView 在我使用取消按钮取消 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];
}

【问题讨论】:

  • 我们可以看一些代码吗?也许你对-alertViewCancel 的实现,以及处理你的警报视图的其他sn-ps(比如你第一次展示它的地方)。
  • 好的。对不起,我忘记了。我现在编辑了我的问题。

标签: iphone methods delegates uialertview


【解决方案1】:

alertViewCancel 用于当系统关闭您的警报视图时,而不是当用户按下“取消”按钮时。引用apple docs:

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

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

index = alertDone.cancelButtonIndex;

【讨论】:

  • 非常感谢 pheelick。我以前读过它,但我没有明白那不是我想要的,因为我的英语不是那么好。
  • 别难过,我昨天也有同样的误解,而且我是母语人士。我发送了反馈,提到了令人困惑的措辞。如果我们中的更多人这样做,也许其他人就不会像我们一样被绊倒了。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      相关资源
      最近更新 更多