8

我遇到的问题是 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];
}
4

3 回答 3

12

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

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

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

index = alertDone.cancelButtonIndex;
于 2010-03-15T15:50:16.920 回答
2

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

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
      //cancel button clicked. Do something here.
    }
    else{
      //other button indexes clicked
    }
}   
于 2010-03-16T15:18:22.193 回答
0

这可以通过两种方式改进。首先,它只处理用户实际点击按钮的情况。它不处理调用 [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
    }
}   
于 2013-10-17T18:49:29.850 回答