解散Modal View Controller iOS

时间:2018-07-23 13:35:00

标签: ios iphone swift xcode uitabbarcontroller

我想实现login(modal)和Tab Bar项目(VCs),但遇到麻烦。解散modalVC后,我无法切换到选项卡或TabBarController。 整个实现以编程方式完成。在main.StoryBoard中,我的根VC只是空白的Login VC。

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

发生了什么事? -> LoginModal被解雇,但没有切换到任何选项卡或TabViewController,或者我不知道发生了什么。我在下面附上正在发生的事情的屏幕截图。 {关闭modalVC后的视图} The View after dismissing the modal.

所以, 我有一个带有“登录”功能的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) 
    }

1 个答案:

答案 0 :(得分:1)

我认为当该控制器后面没有任何内容可显示时,您正在关闭LoginVC。

将根视图控制器从loginVC更改为tabBarVC。在tabBarVC的viewWillAppear方法中,执行逻辑以检查它是否登录过。如果尚未登录,请使用LoginVC搜索(或显示)用户,然后在登录时解雇用户。如果已经登录,请跳过LoginVC选择并显示您的TabBarVC。