我想实现登录(模态)和选项卡栏项(VC)但卡住了。关闭 modalVC 后,我无法切换到选项卡或 TabBarController。 整个实现以编程方式完成。只是一个空白的登录 VC 是我在 main.StoryBoard 中的根 VC。
我想要的 -> 成功时关闭 LoginModal 并切换到 TabBarController 的 Tab1。
这是怎么回事? -> LoginModal 被关闭但没有切换到任何选项卡或 TabViewController 或者我不知道发生了什么。我在下面附上了正在发生的事情的屏幕截图。 {关闭 modalVC 后的 View }
所以, 我有一个带有“登录”功能的 LoginViewController ->
//LoginController.swift
func login() {
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let mainNavigationController = rootViewController as? MainNavigationController else { return }
mainNavigationController.tabBarController?.selectedIndex = 1
UserDefaults.standard.setIsLoggedIn(value: true)
dismiss(animated: true, completion: nil)
}
我创建了 TabBarController 类如下->
//TabBarController.swift
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = HomeController()
firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
let secondViewController = EventsViewController()
secondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 1)
let tabBarList = [firstViewController, secondViewController, thirdViewController, fourthViewController]
viewControllers = tabBarList
}
最后 MainNavigationController 如下->
//MainNavigationController.swift
class MainNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
if isLoggedIn() {
//assume user is logged in
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let mainNavigationController = rootViewController as? TabBarController else { return }
mainNavigationController.tabBarController?.selectedIndex = 1
} else {
//present LoginVC modally
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
最佳答案
我认为当 Controller 后面没有任何内容可显示时,您正在关闭 LoginVC。
将您的 Root View Controller 从 loginVC 更改为您的 tabBarVC。在 tabBarVC 的 viewWillAppear 方法中,执行检查用户是否登录的逻辑。如果未登录,则使用 LoginVC 继续(或呈现)用户并在他们登录时关闭。如果他们已经登录,则跳过 LoginVC 继续并显示您的 TabBarVC。
关于ios - 关闭模态视图 Controller iOS 后无法切换到 TabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51480136/