我遇到的问题是 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 用于系统关闭警报视图时,而不是当用户按下“取消”按钮时。引用自 苹果文档:

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

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

index = alertDone.cancelButtonIndex;

其他提示

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

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

这可以通过两种方式加以改进。首先,它只能处理用户实际点击一个按钮的情况。它不处理的情况是[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
    }
}   
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top