在clickedButtonAtIndex中显示警报?

时间:2011-01-27 16:49:55

标签: iphone objective-c ipad uinavigationcontroller uialertview


我需要在用户按下buttonIndex 1后显示确认提醒,但是......如果我在popViewcontroller中使用clickedButtonAtIndex它会崩溃而不会出错。

问题在于

[self.navigationController popViewControllerAnimated:YES];
在第二次警报点击之前调用

...

如何解决?

这是我的代码:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        UIAlertView *alert = 
            [[UIAlertView alloc] initWithTitle:@"OK!"
                                    message:@"Completed"
                                    delegate:self 
                    cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show]; 
            [alert release];

            [self.navigationController popViewControllerAnimated:YES];
    }
}

1 个答案:

答案 0 :(得分:3)

将两个UIAlertViews的标签属性分别设置为1和2。然后,在delegate方法中,使用if语句检查UIAlertView参数的标记。

示例:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1)
    {
        //check the button index
        //create and display the other alert view (set the tag property here to 2)
    }
    else if (alertView.tag == 2)
    {
        //pop the view controller
    }
}