IOS:使用UIAlert的cancelButtonTitel删除子视图

时间:2011-04-18 14:19:14

标签: objective-c xcode ios uialertview cancel-button

我可以使用UIAlert的cancelButtonTitle删除子视图吗?因为我写这个:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Done!" 
                                                    message:[@"It's all ok!"]
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];

[self dismissModalViewControllerAnimated:YES];

但是这个代码在推送cancelButtonTitle之前删除了我的子视图,我该怎么办?

2 个答案:

答案 0 :(得分:2)

是的,实施UIAlertViewDelegate方法alertView:didDismissWithButtonIndex:并在那里解除您的观点。

将委托设置为自己:

...
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Done!" 
                                                    message:[@"It's all ok!"]
                                                   delegate:self
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
...

看起来像这样:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
    if (buttonIndex == [alertView cancelButtonIndex]) {
        [self dismissModalViewControllerAnimated:YES];
    }
}

答案 1 :(得分:0)

UIAlertView的委托设置为self并实现:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

在此方法中,关闭视图。