【问题标题】:Possible to use UIAlertController on a modal view controller?可以在模态视图控制器上使用 UIAlertController 吗?
【发布时间】:2016-09-16 18:04:32
【问题描述】:

我有一个呈现的(模态)视图控制器,它需要呈现一个操作表。但是,当我这样做时:

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

...

[self presentViewController:actionSheet animated:YES completion:nil];

我明白了:

Warning: Attempt to present <UIAlertController: 0x7fed4a608120>  on <MyViewController: 0x7fed4d1ca520> which is already presenting (null)

这里有解决方法吗?从呈现的视图控制器中使用警报或操作表似乎是一种非常常见的场景。

【问题讨论】:

  • 您在何时何地尝试调用问题中的代码?显示更多上下文。
  • @rmaddy 这是在呈现的视图控制器上按下按钮时。
  • 这应该可以。问题不在这里。您可以创建一个示例项目,该项目显示一个模式,该模式又显示一个操作表。如果失败,请发布代码。

标签: ios uiviewcontroller uiactionsheet uialertcontroller


【解决方案1】:

我认为您正在尝试显示来自 viewDidLoad 的当前视图控制器的操作表。这就是在控制器中未显示操作表的原因,因为控制器的视图尚未加载。因此,如果您这样做,则编写这些代码以在您的 voewDidAppear 方法中显示操作表,它将按预期向您显示操作表。

这里是代码 sn-p。我已经对其进行了测试,并且可以正常工作。

代码:

- (void)viewDidAppear:(BOOL)animated
{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Hello" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }];

    [actionSheet addAction:ok];

    [self presentViewController:actionSheet animated:YES completion:nil];

}

编码愉快...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多