【问题标题】:Warning: Attempt to present <UIImagePickerController: > on <...> which is already presenting (null)警告:尝试在已呈现的 <...> 上呈现 <UIImagePickerController: > (null)
【发布时间】:2016-11-29 11:59:31
【问题描述】:

我在这里寻求帮助!

我在 swift 中有一个 ActionController,我试图从创建的其中一个动作中呈现一个 UIImagePickerController,但是当我运行它时,它向我显示了这个警告:尝试在 <...> 上呈现,它已经呈现(null),并且它没有呈现任何东西。

这是我的代码:

    let actionController = TweetbotActionController()



        actionController.addAction(Action("Photo Library", style: .Default , handler: { action in
            let picker = UIImagePickerController()
            picker.sourceType = .PhotoLibrary
            picker.allowsEditing = false
            picker.delegate = self

            self.presentViewController(picker, animated: true, completion: nil)


        }))

    actionController.addAction(Action("Take Photo", style: .Default, handler: {action in

        NSLog("Take Photo Pressed")
    }))

    actionController.addSection(Section())
    actionController.addAction(Action("Cancel", style: .Cancel, handler:nil))

    presentViewController(actionController, animated: true, completion:nil)

非常感谢!!!

【问题讨论】:

  • 我试过这个: if self.presentedViewController == nil { self.presentViewController(picker, animated: true, completion: nil) } 但它消除了警告但不显示pickerController。跨度>
  • 您在关闭 ActionController 之前呈现 ImagePicker。尝试关闭“actionController”,然后在完成处理程序中打开 ImagePicket。
  • 你的意思是“关闭actionController”使用这个:dispatch_async(dispatch_get_main_queue() ?
  • 没有类似的东西 "dismissViewControllerAnimated(true) { // 在此处打开 ImagePicker } "
  • 知道了!!!它完美地工作。感谢您的帮助

标签: swift


【解决方案1】:

错误消息是不言自明的。您不能同时呈现两个视图控制器。

您应该先关闭AlertController,然后再打开新的。为了实现这一点,您有多种选择,但我相信最简单的方法是在您按下“照片库”并在那里设置某种 flag 时关闭 AlertController。这可以是一个名为 shouldOpenPicker 的变量,除非您按下按钮,否则它将始终为 false。

然后您可以使用presentViewController(提供AlertController 的那个)的completion 闭包在标志变量设置为true 的情况下打开选择器控制器。如果是,则显示选择器控制器并再次将标志设置为false

更新:

您应该在关闭第一个视图控制器时显示第二个视图控制器,如下所示:

alert.addAction(UIAlertAction(title: "Photo library", style: .Default, handler: { action in
    alert.dismissViewControllerAnimated(true, completion: {
        let picker = UIImagePickerController()
        picker.sourceType = .PhotoLibrary
        picker.allowsEditing = false
        picker.delegate = self

        self.presentViewController(picker, animated: true, completion: nil)
    })
}))

【讨论】:

  • 是的,flag的用法我明白了,但是不太清楚的是如何使用AlertController presentViewController的完成闭包来打开picker控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
相关资源
最近更新 更多