Attempt to present on whose view is not in ...

问题描述

当从一个controller present 到 webviewcontroller,加载的html弹框选取照片时,选取之后并没有弹出imagepickcontroller,而且当前的webviewcontroller也会dismiss掉,并且控制台输出错误内容

Warning: Attempt to present on whose view is not in the window hierarchy!

Attempt to present <UIImagePickerController > on <UINavigationController: > whose view is not in ..._第1张图片
Simulator Screen Shot - iPhone X - 2018-10-25 at 17.06.31.png

在我们重写了dismiss方法

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    print("test")
    super.dismiss(animated: flag, completion: completion)
}

会发现test输出了两次


Attempt to present <UIImagePickerController > on <UINavigationController: > whose view is not in ..._第2张图片
Snip20181025_1.png

UIWebview使UIAlertcontrollerUIWebviewviewcontroller上显示以显示相机/现有/取消。当用户选择选项时,UIAlertcontroller意味着被释放并且UIImagepickercontroller被呈现。然而,对UIAlertcontroller的释放似乎调用了两次。第一次是好的,因为presentviewcontroller =UIAlertcontroller,但第二次presentviewcontroller属性为nil,这导致dismissviewcontroller animated方法杀死UIWebviewviewcontroller

解决方法:

  • 方法一
    使用push到webviewcontroller,这是最简单的方法了

  • 方法二

在自定义的UINavigationcontroller做些操作

class XYNavigationViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.view.window?.rootViewController = self
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    print("test")
    if let vc = self.presentedViewController {
        // don't bother dismissing if the view controller being presented is a doc/image picker
        if !vc.isKind(of: UIDocumentMenuViewController.self) || !vc.isKind(of:UIImagePickerController.self) {
            super.dismiss(animated: flag, completion:completion)
        }
    }

}

}

参考链接地址:https://stackoverflow.com/questions/25942676/ios-8-sdk-modal-uiwebview-and-camera-image-picker

你可能感兴趣的:(Attempt to present on whose view is not in ...)