在同一个标签栏项目上加载新的 viewController 无效
Loading a new viewController on the same tab bar item not working
所以我在同一个标签栏项目上加载新的视图控制器时遇到了问题。我有一个标签栏控制器作为我的根控制器,标签栏上有 4 个标签。其中之一是帐户选项卡,当用户登录时,他需要查看其帐户详细信息的概览,但是当没有用户时,该选项卡栏项目上需要有一个登录/登录页面。目前,我的 tabbarcontroller 有一个自定义选项卡栏 class,它具有我认为是解决方案但没有任何反应的功能。我在需要加载的视图控制器(用户详细信息页面)中放置了一个断点,它出现在 ViewDidLoad 中,因此它加载但没有出现在屏幕上。
希望我能最终解决这个问题!
亲切的问候
B.
这是我的自定义标签栏控制器class:
import UIKit
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
var window: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 3{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
window?.rootViewController = loginVC
window?.makeKeyAndVisible()
}
return true// you decide
}
}
更改 rootViewController 在 Swift 中是不好的做法。
处理此类任务的正确方法是覆盖您的 accountViewController 的 viewWillAppear 函数以确定用户是否未登录并显示您的 loginViewController。尝试类似的东西:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// authManager would be a custom class that manages authorization with a property for loggedIn
if authManager.loggedIn == false {
guard let loginVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "login") as? LoginViewController else { return }
self.present(loginVC, animated: true, completion: nil)
}
}
这里是呈现全屏登录Vc的代码
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 1{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: false, completion: nil)
}
return true// you decide
}
如果您还想要底栏...您的 viewController 应该是导航控制器,您可以按您想要的 ViewController 然后点赞
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 1{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
// loginVC.modalPresentationStyle = .fullScreen
if let navigation = viewController as? UINavigationController {
navigation.pushViewController(loginVC, animated: false)
}
}
return true// you decide
}
它将在顶部显示后退按钮和导航栏
如果你不想要 NavigationBar 那么在你的 MessagesViewController
中加载函数调用
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
// Do any additional setup after loading the view.
}
如果你只想隐藏后退按钮,请调用
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: false);
// Do any additional setup after loading the view.
}
所以我在同一个标签栏项目上加载新的视图控制器时遇到了问题。我有一个标签栏控制器作为我的根控制器,标签栏上有 4 个标签。其中之一是帐户选项卡,当用户登录时,他需要查看其帐户详细信息的概览,但是当没有用户时,该选项卡栏项目上需要有一个登录/登录页面。目前,我的 tabbarcontroller 有一个自定义选项卡栏 class,它具有我认为是解决方案但没有任何反应的功能。我在需要加载的视图控制器(用户详细信息页面)中放置了一个断点,它出现在 ViewDidLoad 中,因此它加载但没有出现在屏幕上。
希望我能最终解决这个问题!
亲切的问候 B.
这是我的自定义标签栏控制器class:
import UIKit
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
var window: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 3{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
window?.rootViewController = loginVC
window?.makeKeyAndVisible()
}
return true// you decide
}
}
更改 rootViewController 在 Swift 中是不好的做法。
处理此类任务的正确方法是覆盖您的 accountViewController 的 viewWillAppear 函数以确定用户是否未登录并显示您的 loginViewController。尝试类似的东西:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// authManager would be a custom class that manages authorization with a property for loggedIn
if authManager.loggedIn == false {
guard let loginVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "login") as? LoginViewController else { return }
self.present(loginVC, animated: true, completion: nil)
}
}
这里是呈现全屏登录Vc的代码
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 1{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: false, completion: nil)
}
return true// you decide
}
如果您还想要底栏...您的 viewController 应该是导航控制器,您可以按您想要的 ViewController 然后点赞
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 1{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
// loginVC.modalPresentationStyle = .fullScreen
if let navigation = viewController as? UINavigationController {
navigation.pushViewController(loginVC, animated: false)
}
}
return true// you decide
}
它将在顶部显示后退按钮和导航栏
如果你不想要 NavigationBar 那么在你的 MessagesViewController
中加载函数调用
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
// Do any additional setup after loading the view.
}
如果你只想隐藏后退按钮,请调用
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: false);
// Do any additional setup after loading the view.
}