ios - 可以在模态视图 Controller 上使用 UIAlertController 吗?

标签 ios uiviewcontroller uiactionsheet uialertcontroller

我有一个呈现的(模态) View Controller ,需要呈现一个操作表。但是当我这样做时:

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)

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

最佳答案

我认为您正在尝试显示呈现 View Controller 的 viewDidLoad 中的操作表。这就是 Controller View 尚未加载时未在 Controller 中显示操作表的原因。因此,如果您这样做,请编写这些代码以在您的 voewDidAppear 方法中显示操作表,它会按预期显示操作表。

这是代码片段。我已经对其进行了测试,它正在运行。

代码:

- (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];

}

快乐编码......

关于ios - 可以在模态视图 Controller 上使用 UIAlertController 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39537621/

相关文章:

ios - iOS 中的宏录制

ios - 代码: "Revoke certificate - Your account already has a signing certificate for this machine but it is not present in your keychain."

Swift:从另一个类调用时 UIPanGestureRecognizer 不起作用

ios - 如何在 iOS 8 中自定义 UIAction Sheet

ios - 旋转的 UIActionsheet

ios - 无法本地化 iOS 应用程序包显示名称

ios - kCGBitmapByteOrder32Little 是什么意思

iphone - UIAction Sheet 和 Alert 问题

ios - 为什么 webview 不适合所有 View ?

ios - 什么时候调用 ViewWillLayoutSubviews 和 ViewDidLayoutSubviews 方法?