我有一個tabBarController,其中一個選項卡是一個名爲view2的視圖。在view2中有一些代碼在viewWillAppear中運行。我在view2中也有一個UIImagePickerController。Swift iOS -In ViewWIllAppear如何檢查ImagePicker在切換選項卡或視圖被打開/關閉時是否呈現或取消?
fileprivate var imagePicker = UIImagePickerController()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
runSomeCode()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
imageView.image = image
imagePicker.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
imagePicker.dismiss(animated: true, completion: nil)
}
我知道每次imagePicker呈現然後駁回然後viewWillAppear中runSomeCode時間內()運行。我不希望它繼續被調用。
我最初使用下面的代碼,但我意識到viewWillAppear中的代碼只有在view2被推入時纔會運行。如果我切換標籤並返回它將不會運行。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (isMovingToParentViewController) || (isBeingPresented){
runSomeCode()
}else{
//runs when switching tabs but also runs after the imagePicker is dismissed
}
}
我如何檢查imagePicker正在呈現或辭退,使我在viewWillAppear中的代碼將不能運行?
退房這個答案一個類似的問題。 https://stackoverflow.com/a/31686885/50255 –
@ChrisAllwein謝謝:)爲什麼你不把這個鏈接作爲接受的答案,所以我可以更新你!這正是我所尋找的 –