如何将 rootviewcontroller 更改为堆栈中的第三个 viewcontroller

How to change the rootviewcontroller to the third viewcontroller in a stack

var window: UIWindow?



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let tabBarController = UITabBarController()

        let settingStoryboard = UIStoryboard(name: "Setting", bundle: nil)
        let toDoListStoryboard = UIStoryboard(name: "ToDoList", bundle: nil)
        let homeStoryboard = UIStoryboard(name: "Home", bundle: nil)
        let eventListStoryboard = UIStoryboard(name: "EventList", bundle: nil)
        let activityStoryboard = UIStoryboard(name: "Activity", bundle: nil)

        let settingVC = settingStoryboard.instantiateViewController(withIdentifier: "Setting") as! SettingViewController
        let toDoListVC = toDoListStoryboard.instantiateViewController(withIdentifier: "ToDoList") as! ToDoListViewController
        let homeVC = homeStoryboard.instantiateViewController(withIdentifier: "Home") as! HomeViewController
        let eventListVC = eventListStoryboard.instantiateViewController(withIdentifier: "EventList") as! EventListViewController
        let activityVC = activityStoryboard.instantiateViewController(withIdentifier: "Activity") as! ActivityViewController

        let vcData: [(UIViewController, UIImage, UIImage)] = [

            (settingVC, UIImage(named: "SettingIcon")!, UIImage(named: "SettingSelectedIcon")!),
            (toDoListVC, UIImage(named: "ToDoIcon")!, UIImage(named: "ToDoSelectedIcon")!),
            (homeVC, UIImage(named: "HomeIcon")!, UIImage(named: "HomeSelectedIcon")!),
            (eventListVC, UIImage(named: "EventIcon")!, UIImage(named: "EventSelectedIcon")!),
            (activityVC, UIImage(named: "ActivityIcon")!, UIImage(named: "ActivitySelectedIcon")!)
        ]

        let vcs = vcData.map { (vc, image, selectedImage) -> UINavigationController in

            let nav = UINavigationController(rootViewController: vc)

            nav.tabBarItem.image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            nav.tabBarItem.selectedImage = selectedImage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)

            nav.tabBarItem.imageInsets = UIEdgeInsets(top: 7, left: 0, bottom: -5, right: 0)

            return nav
        }

        tabBarController.viewControllers = vcs
        tabBarController.tabBar.isTranslucent = false
        tabBarController.tabBar.barTintColor = UIColor(red: 19/255, green: 41/255, blue: 77/255, alpha: 1)

        UINavigationBar.appearance().barTintColor = UIColor(red: 19/255, green: 41/255, blue: 77/255, alpha: 1)
        UINavigationBar.appearance().isTranslucent = false

        window?.rootViewController = tabBarController

        print(Realm.Configuration.defaultConfiguration.fileURL!)

        return true
    }

在上面显示的代码中,当应用程序运行时,rootviewcontroller 是第一个添加到堆栈的视图。在不先初始化 homeviewcontroller 的情况下,如何将 rootviewcontroller 更改为 HomeViewController。

只需添加

tabBarController.selectedIndex = 2

在你的行之后

tabBarController.viewControllers = vcs

您可以通过以下方式随时更改 RootViewController

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let homeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
UIApplication.shared.delegate?.window??.rootViewController = homeViewController