【问题标题】:Unable to switch to TabBarController after dismissing Modal View Controller iOS关闭模态视图控制器 iOS 后无法切换到 TabBarController
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 2020-04-25
      • 2014-04-06
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多