来自 Storyboard 的实例化 NavigationController 引用相同的 RootViewController

Instantiated NavigationController from storyboard reference the same RootViewController

我正在从情节提要中实例化一个导航控制器,我已经为其定义了根视图控制器。

如果我实例化多个导航控制器如下:

        let nav = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CategorizedArticleNavigationController") as! UINavigationController
        let rootVC = nav.viewControllers.first as! CategorizedArticlesViewController
        let nav_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CategorizedArticleNavigationController") as! UINavigationController
        let rootVC_1 = nav.viewControllers.first as! CategorizedArticlesViewController            
        self.present(rootVC, animated: true, completion: nil)

我尝试以模态方式呈现后出现异常:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller ...'

调试后,令我惊讶的是,我发现 nav != nav_1

但是

rootVC == rootVC_1

然后我得到一个异常,试图以模态方式呈现导航,因为它发现它的根 vc 已经被呈现。

有人看过吗?我应该提交错误吗?或者这是故意的?

更新:这绝对是我的错误,@Samantha hepled 澄清

谢谢。

我认为它们相同的原因是这一行:

let rootVC_1 = nav.viewControllers.first as! CategorizedArticlesViewController            

在这里,您将 rootVC_1 分配给原始导航控制器的根视图控制器(因此,与 rootVC 相同的对象)。应该是

let rootVC_1 = nav_1.viewControllers.first as! CategorizedArticlesViewController