【发布时间】:2018-12-30 23:38:21
【问题描述】:
我想实现登录(模式)和标签栏项目(VC),但卡住了。关闭 modalVC 后,我无法切换到选项卡或 TabBarController。 整个实现以编程方式完成。只是一个空白的登录 VC 是我在 main.StoryBoard 中的根 VC。
我想要什么 -> 成功关闭 LoginModal 并切换到 TabBarController 的 Tab1。
发生了什么事? -> LoginModal 被解除但没有切换到任何选项卡或 TabViewController 或者我不知道发生了什么。我在下面附上了正在发生的事情的屏幕截图。 {关闭modalVC后的视图}
所以, 我有一个带有“登录”功能的 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)
}
【问题讨论】:
标签: ios iphone swift xcode uitabbarcontroller