【问题标题】:IOS: remove a subview with cancelButtonTitel of UIAlertIOS:使用 UIAlert 的 cancelButtonTitel 删除子视图
【发布时间】:2011-04-18 14:19:13
【问题描述】:

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

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

[self dismissModalViewControllerAnimated:YES];

但是这段代码在我按下cancelButtonTitle之前删除了我的子视图,我该怎么办?

【问题讨论】:

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


    【解决方案1】:

    是的,实现 UIAlertViewDelegate 方法 alertView:didDismissWithButtonIndex: 并在那里关闭您的视图。

    先将委托设置为self:

    ...
    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];
        }
    }
    

    【讨论】:

    • Protip:如果您不确定cancelButtonIndex,请使用if (buttonIndex == [alertView cancelButtonIndex]) :-)
    • 但是我必须调用这个方法...不是吗? [self alertView:alertView clickedButtonAtIndex:?????];我必须在 clickedButtonAtIndex 中写什么???
    • @fluchtpunkt 谢谢,我一直在使用自己的常量
    【解决方案2】:

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

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

    在此方法中,关闭视图。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多