关闭模式视图控制器iOS后无法切换到TabBarController

关闭模式视图控制器iOS后无法切换到TabBarController,ios,iphone,swift,xcode,uitabbarcontroller,Ios,Iphone,Swift,Xcode,Uitabbarcontroller,我想实现loginmodal和tabbar itemsvc,但遇到了问题。关闭modalVC后,我无法切换到选项卡或TabBarController。 整个实现是以编程方式完成的。只是一个空白的登录VC是我在main.StoryBoard中的根VC 我想要的->成功后关闭LoginModel并切换到TabBarController的Tab1 发生了什么事?->LoginModel被取消,但没有切换到任何选项卡或TabViewController,或者我不知道发生了什么。我附上下面发生的事情的截图

我想实现loginmodal和tabbar itemsvc,但遇到了问题。关闭modalVC后,我无法切换到选项卡或TabBarController。 整个实现是以编程方式完成的。只是一个空白的登录VC是我在main.StoryBoard中的根VC

我想要的->成功后关闭LoginModel并切换到TabBarController的Tab1

发生了什么事?->LoginModel被取消,但没有切换到任何选项卡或TabViewController,或者我不知道发生了什么。我附上下面发生的事情的截图。{推翻莫达尔夫委员会后的观点}

所以, 我有一个带有“登录”功能的LoginView控制器->

//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.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) 
    }
最后是MainNavigationController,如下所示->

//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.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) 
    }

我想你是在解雇你的LoginVC,因为控制器后面没有任何东西可以显示

将根视图控制器从loginVC更改为tabBarVC。在tabBarVC的viewWillExample方法中,执行逻辑检查用户是否登录。如果未登录,请向用户发送或提供LoginVC,并在用户登录时关闭。如果他们已经登录,请跳过LoginVC segue并显示您的TabBarVC