問題描述
當(dāng)從一個(gè)controller present 到 webviewcontroller灶轰,加載的html彈框選取照片時(shí),選取之后并沒有彈出imagepickcontroller丙笋,而且當(dāng)前的webviewcontroller也會dismiss掉,并且控制臺輸出錯(cuò)誤內(nèi)容
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)
}
會發(fā)現(xiàn)test輸出了兩次
UIWebview
使UIAlertcontroller
在UIWebviewviewcontroller
上顯示以顯示相機(jī)/現(xiàn)有/取消续膳。當(dāng)用戶選擇選項(xiàng)時(shí)坯临,UIAlertcontroller
意味著被釋放并且UIImagepickercontroller
被呈現(xiàn)。然而故痊,對UIAlertcontroller
的釋放似乎調(diào)用了兩次顶瞳。第一次是好的,因?yàn)?code>presentviewcontroller =UIAlertcontroller愕秫,但第二次presentviewcontroller
屬性為nil慨菱,這導(dǎo)致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