UITabbarController 替换最后一个视图控制器
UITabbarController replace the last view controller
我需要根据情况更换最后一个视图控制器。进入 TabBarController
我有 4 个视图控制器,最后一个视图控制器是 LoginVC
。如果我已经登录,我需要将 LoginVC
替换为 ProfileVC
。
我试试:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedIndex == 3 && !DefaultsManager.instance.getUserToken().isEmpty {
showProfile()
return false
}
return true
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 3 && !DefaultsManager.instance.getUserToken().isEmpty {
showProfile()
}
}
func showProfile() {
let profile = ProfileVC.instantiateNCFromStoryboard()
self.viewControllers?[3] = profile
}
但这对我不起作用。
带有登录屏幕的屏幕
屏幕个人资料屏幕符合我的条件
在AppDelegate
中检查用户登录状态并更改最后一个视图控制器及其tabBarItem
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let rootVC = self.window?.rootViewController as? UITabBarController, var viewControllers = rootVC.viewControllers {
viewControllers.removeLast()
if userLoggedIn {
let profileVC = ProfileVC()
//profileVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
profileVC.tabBarItem = UITabBarItem(title: "Profile", image: nil, selectedImage: nil)
viewControllers.append(profileVC)
} else {
let loginVC = LoginVC()
//loginVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
loginVC.tabBarItem = UITabBarItem(title: "Login", image: nil, selectedImage: nil)
viewControllers.append(loginVC)
}
rootVC.viewControllers = viewControllers
}
return true
}
在成功登录后的LoginVC
中,从tabBarController
中删除LoginVC
并添加ProfileVC
@objc func loginBtnAction(_ sender: UIButton) {
if var viewControllers = self.tabBarController?.viewControllers {
viewControllers.removeLast()
let profileVC = ProfileVC()
//profileVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
profileVC.tabBarItem = UITabBarItem(title: "Profile", image: nil, selectedImage: nil)
viewControllers.append(profileVC)
self.tabBarController?.viewControllers = viewControllers
}
}
在 ProfileVC
的 Logout
按钮操作中,从 tabBarController
中删除 ProfileVC
并添加 LoginVC
@objc func logoutBtnAction(_ sender: UIButton) {
if var viewControllers = self.tabBarController?.viewControllers {
viewControllers.removeLast()
let loginVC = LoginVC()
//loginVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
loginVC.tabBarItem = UITabBarItem(title: "Login", image: nil, selectedImage: nil)
viewControllers.append(loginVC)
self.tabBarController?.viewControllers = viewControllers
}
}
我需要根据情况更换最后一个视图控制器。进入 TabBarController
我有 4 个视图控制器,最后一个视图控制器是 LoginVC
。如果我已经登录,我需要将 LoginVC
替换为 ProfileVC
。
我试试:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedIndex == 3 && !DefaultsManager.instance.getUserToken().isEmpty {
showProfile()
return false
}
return true
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 3 && !DefaultsManager.instance.getUserToken().isEmpty {
showProfile()
}
}
func showProfile() {
let profile = ProfileVC.instantiateNCFromStoryboard()
self.viewControllers?[3] = profile
}
但这对我不起作用。
带有登录屏幕的屏幕
屏幕个人资料屏幕符合我的条件
在AppDelegate
中检查用户登录状态并更改最后一个视图控制器及其tabBarItem
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let rootVC = self.window?.rootViewController as? UITabBarController, var viewControllers = rootVC.viewControllers {
viewControllers.removeLast()
if userLoggedIn {
let profileVC = ProfileVC()
//profileVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
profileVC.tabBarItem = UITabBarItem(title: "Profile", image: nil, selectedImage: nil)
viewControllers.append(profileVC)
} else {
let loginVC = LoginVC()
//loginVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
loginVC.tabBarItem = UITabBarItem(title: "Login", image: nil, selectedImage: nil)
viewControllers.append(loginVC)
}
rootVC.viewControllers = viewControllers
}
return true
}
在成功登录后的LoginVC
中,从tabBarController
中删除LoginVC
并添加ProfileVC
@objc func loginBtnAction(_ sender: UIButton) {
if var viewControllers = self.tabBarController?.viewControllers {
viewControllers.removeLast()
let profileVC = ProfileVC()
//profileVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
profileVC.tabBarItem = UITabBarItem(title: "Profile", image: nil, selectedImage: nil)
viewControllers.append(profileVC)
self.tabBarController?.viewControllers = viewControllers
}
}
在 ProfileVC
的 Logout
按钮操作中,从 tabBarController
中删除 ProfileVC
并添加 LoginVC
@objc func logoutBtnAction(_ sender: UIButton) {
if var viewControllers = self.tabBarController?.viewControllers {
viewControllers.removeLast()
let loginVC = LoginVC()
//loginVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
loginVC.tabBarItem = UITabBarItem(title: "Login", image: nil, selectedImage: nil)
viewControllers.append(loginVC)
self.tabBarController?.viewControllers = viewControllers
}
}