以编程方式在tabitem上加载navigationController

时间:2018-10-15 10:48:34

标签: ios swift uitabbarcontroller

我已经制作了一个UITabBarController,在其中选择一项时,我会加载一个图像选择器。现在,当用户从选择器中选择一个图像后,我想加载一个导航控制器,但我不能。我的代码是

extension BaseTabBarController : UITabBarControllerDelegate {

func  tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if self.viewControllers?.index(of:viewController) == 2 {
        // Presenting image picker here
        present(self.imagePicker, animated: true, completion: nil)
        return false
    } else {
        return true
    }
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

 }

}

这是用户选择图片后的代码

extension BaseTabBarController : ImagePickerDelegate {

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage])  {

    //This is the StoryboardID of Navigationcontroller i want to goto
    let detailVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "UploadNavigationController")
    //This is not working
    present(detailVC, animated: true,completion: nil)
 }
}

此处未显示控制器,我收到此警告

Warning: Attempt to present <UINavigationController: 0x7fb4969c4800> on <BaseTabBarController: 0x7fb49780ea00> whose view is not in the window hierarchy!

我想显示导航控制器,同时标签栏仍在底部可见

1 个答案:

答案 0 :(得分:0)

我想,当您要显示导航控制器时标签栏仍在底部可见时,那么您应该首先关闭图像选择器,然后再推不存在的新控制器。

因为如果您呈现任何控制器,它将像从根控制器呈现一样覆盖整个屏幕。如果您按下该按钮,它将从当前控制器进行推送,并且tabor保持不变。

extension BaseTabBarController : ImagePickerDelegate {

    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        self.imagePicker.dismiss(animated: true) {
        //This is the StoryboardID of Navigationcontroller i want to goto
        let detailVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "UploadNavigationController")
        //This is not working
        let nav = UINavigationController(rootViewController: detailVC)
        navigationController?.pushViewController(nav, animated: true)
        //present(detailVC, animated: true,completion: nil)
    }
}