问题描述
当从一个controller present 到 webviewcontroller,加载的html弹框选取照片时,选取之后并没有弹出imagepickcontroller,而且当前的webviewcontroller也会dismiss掉,并且控制台输出错误内容
Warning: Attempt to present <UIImagePickerController: 0x7fe7fc008600> on <Test.XYNavigationViewController: 0x7fe7fc860a00> whose view is not in the window hierarchy!
在我们重写了dismiss方法
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
print("test")
super.dismiss(animated: flag, completion: completion)
}
会发现test输出了两次
UIWebview
使UIAlertcontroller
在UIWebviewviewcontroller
上显示以显示相机/现有/取消。当用户选择选项时,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