首页 > 解决方案 > UIImagePickerController 关闭不正确

问题描述

UITabbarController中有5个项目:VC0、VC1、VC3(默认选中)、VC4、VC5。

UITabbarController > NavigationController > VC1 > VC1-A

UIImagePickerController 在 VC1-A 中呈现:

let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
imagePickerController.allowsEditing = false
self.present(imagePickerController, animated: false)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)

    if let image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage{
       topImage.image = image
    }

    picker.dismiss(animated: true, completion: nil)

}

When image is selected and the picker is dismissed, the view will go to the default selected item VC3, but not VC1-A.

我在 App 的重新测试中发现了这一点,但我记得它之前工作正常。我现在不记得我做了什么可能使它不正确,Xcode 更新,Swift 版本更新?我知道我在这部分没有做任何改变。

以下尝试也不起作用:

self.navigationController?.present(imagePickerController, animated: false)
self.navigationController?.dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)
picker.popViewController(animated: true)

现在我让它像这样工作:

picker.dismiss(animated: true, completion: {
        self.tabBarController?.selectedIndex = 1
    })

但是解决问题不是一个好主意,希望有人能给出一个适当的解决方案。谢谢。

标签: swiftuinavigationcontrolleruitabbarcontrolleruiimagepickercontrollerdismiss

解决方案


您应该获取 VC1-A 导航控制器的 tabBarController 并更改选定的索引

picker.dismiss(animated: true) {//dismiss image picker
    if let navigationController = self.navigationController {
        navigationController.popToRootViewController(animated: true)//go to VC1
        navigationController.tabBarController?.selectedIndex = 2//go to VC3
    }
}

推荐阅读