我已经创建了一个警报视图使用以下代码两个按钮:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: title 
message: msg delegate:nil cancelButtonTitle:@"Replay" otherButtonTitles:@"Highscore", nil];
[alertView show];

我想时被点击的按钮中的一个来运行一些代码。 为了做到这一点,我已加入下列方法将delegate.m文件:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex==0) //Run some code 
 else //Other code
 }

但这种方法并不当我按下的按钮中的任一称为! 谁能告诉我,为什么?

由于提前,

Sagiftw

有帮助吗?

解决方案

delegate:nil

如何将警报视图的委托,如果你指定不会有代表相关联?与替换部分

delegate:self

代替。

其他提示

尝试委托设置到自代替零。

我打电话UIAlertView中的 dismissWithClickedButtonIndex:动画:从的UITextField委托方法的方法,因为我想处理键盘回车键,以及:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 1001) {
        [textField resignFirstResponder];
        [_alert dismissWithClickedButtonIndex:1 animated:YES];
    }
    return YES;
}

此方式方法的 alertView:clickedButtonAtIndex:永远不会被调用,即使你设置了适当的委托。相反的 alertView:didDismissWithButtonIndex:不会被调用。所以实现此方法来代替:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // your code here
}

如果你只是想把手警报视图的按钮而已,那些第一次调用clickedButtonAtIndex然后didDismissWithButtonIndex。

在.H放UIActionSheetDelegate和在.M在创建alertView把代表自不是nil如在上述情况下正在做

对于这个问题的正确答案是delegate:nil。但如果该委托已被设置为自己和clickedButtonAtIndex仍然没有工作,尝试检查如果你是显示alertview后弹出另一个视图控制器([self.navigationController popViewControllerAnimated:YES];)。这也可能导致对clickedButtonAtIndex不会被调用。这就是发生在我身上。

希望这可以帮助别人。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top