设置根视图控制器然后以编程方式导航到下一个视图

Setting rootViewController then navigate to next view programatically

最初我在登录后有一个层次结构

-> MyCoursesViewController
 -> CourseInfo UITabBarController

如果用户关闭应用程序,然后重新进入,rootViewController 将是正确的 CourseInfo UITabBarController。但是,当用户需要查看不同的课程(退出课程)时,他们无法“返回”到 MyCoursesViewController,因为它不再位于堆栈中。

在 AppDelegate 中:

if (inCourse) {
  let storyboard : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)
  let courseInfoTabController = storyboard.instantiateViewControllerWithIdentifier(“CourseInfo”) as! UITabBarController
  self.window?.rootViewController = courseInfoTabController
} else {
  let storyboard : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)
  let myCoursesViewController = storyboard.instantiateViewControllerWithIdentifier(“MyCourses”)
  self.window?.rootViewController = myCoursesViewController
}

有没有什么方法可以将 MyCoursesViewController 作为 rootViewController 然后自动导航到课程信息 UITabBarController 以便 MyCoursesViewController 位于层次结构中以防他们回击(退出课程)?

或者如果用户退出课程(回击)是否更好,我们以某种方式删除 rootViewController 并替换为新的 rootViewController?另一种选择是,如果我们只是替换 rootViewController,旧的会从内存中释放出来还是仍然在某处引用?

例如

CourseInfo UITabBarController 目前仍然是 rootViewController 但现在我们用一个新的替换它

 let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
 let myCoursesViewController = mainStoryBoard.instantiateViewControllerWithIdentifier(“MyCourses”) as! ViewController
 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
 appDelegate.window?.rootViewController = myCoursesViewController 

在您的 AppDelegate 中,您可以设置层次结构。尝试类似的东西:

    let storyboard : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)

    let myCoursesViewController = storyboard.instantiateViewControllerWithIdentifier(“MyCourses”)

    if isInCourse{
        let courseInfoTabController = storyboard.instantiateViewControllerWithIdentifier(“CourseInfo”) as! UITabBarController
        let navigationBar = UINavigationController()
        navigationBar.setViewControllers([myCoursesViewController,courseInfoTabController], animated: false)
        self.window?.rootViewController = navigationBar
    }else{
        self.window.rootViewController = myCoursesViewController
    }