Ios 如何将rootviewcontroller更改为堆栈中的第三个viewcontroller
在上面显示的代码中,当应用程序运行时,rootviewcontroller是添加到堆栈的第一个视图。如何在不首先初始化HomeViewController的情况下将rootviewcontroller更改为HomeViewController 只需添加Ios 如何将rootviewcontroller更改为堆栈中的第三个viewcontroller,ios,swift,uitabbarcontroller,rootviewcontroller,Ios,Swift,Uitabbarcontroller,Rootviewcontroller,在上面显示的代码中,当应用程序运行时,rootviewcontroller是添加到堆栈的第一个视图。如何在不首先初始化HomeViewController的情况下将rootviewcontroller更改为HomeViewController 只需添加 var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [U
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
}
排队后
tabBarController.selectedIndex = 2
您可以通过以下方式随时更改RootViewController
tabBarController.viewControllers = vcs
运行此代码时,根视图控制器是选项卡栏。你是在问如何使第三个选项卡成为最初选择的选项卡吗?@rmaddy是的,我希望第三个选项卡成为最初的视图控制器。你应该回答你的问题以澄清这一点。Chris下面的答案是您需要的。请阅读问题下方的评论,并阅读其他答案。
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let homeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
UIApplication.shared.delegate?.window??.rootViewController = homeViewController