显示 ViewController,关闭它,然后进入另一个视图

Present a ViewController, dismiss it, and then segue into another View

我正在尝试执行以下操作:

  1. 用户按下按钮
  2. UIImagePickerController弹出
  3. 用户选择图片
  4. UIImagePickerController 被驳回
  5. 进入另一个 ViewController

截至目前,我已将其设置为当用户按下按钮时,初始化 UIImagePickerController 并显示 presentViewController。它还被委托给此按钮也链接到的 class。用户选取图片后,调用imagePickerController,同时调用dismissViewControllerAnimated。然后, performSegueWithIdentifier 被调用。然而,这最后一步抛出以下警告,然后不继续:

2016-09-15 03:45:44.870 Food Diary[9941:1540144] Warning: Attempt to present on whose view is not in the window hierarchy!

我是 Swift 和 iOS 开发的新手,这是什么意思,我该如何解决?

UIImagePickerController

解雇完成后执行 segue
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    // your code
    self.dismissViewControllerAnimated(true, completion: {
        self.performSegueWithIdentifier("SegueIdentifier", sender: self)
    })
}

注意:我已经通过发送者传递了当前的控制器对象,你可以传递任何你想要的对象。

尝试在呈现和关闭视图控制器期间使用这两行代码

呈现:

self.view.window.rootviewcontroller.presentViewController("yourVC",animated:true, nil)

不屑一顾:

self.view.window.rootViewController.dismissViewController(animated:true completion:nil)

它保持不变 ViewController,因为 UIImagePickerController 仍在视图层次结构中,并且第二个 ViewController 无法添加到 UIImagePickerController 之上。要修复,您需要像这样在完成 UIImagePickerCopntroller 解雇时包装您的 segue:

self.dismiss(animated: true, completion: {
    self.performSegue(withIdentifier: "StartToMain", sender: nil)
})