首页 > 解决方案 > 我怎样才能从另一个故事板返回包含 TabBarController 的 Main.storyboard

问题描述

我有两个故事板,一个包含应用程序真正的所有内容,另一个包含我的应用程序的“入职”/教程。

教程完成后,我想导航回我原来的视图控制器。

这是我导航到另一个故事板的代码:

let defaults = UserDefaults.standard
if defaults.bool(forKey: "firstOpened") {
    print("First VC launched")
}else{
    var vc: UIViewController
    let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
    self.present(goTo, animated: true, completion: nil)    
}

有了这个,它就可以工作了,除了 TabBarController 没有显示,也没有按照我想要的方式工作。

这是我导航回 Main.Storyboard 的代码:

@objc func handleSecondPush() {
    //registerForRemoteNotifications() 
    UserDefaults.standard.set(true, forKey: "firstOpened")
    let goTo = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pushToFeedVC") 
    self.present(goTo, animated: true, completion: nil)
    //self.performSegue(withIdentifier: "goToLink", sender: nil)   
}

我也在另一个故事板中尝试过这个,但是这个按钮不会改变视图:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
print(controller)
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
if let tabBarVc = self.window?.rootViewController as? UITabBarController {
    tabBarVc.selectedIndex = 1
}

简短的问题:所以我的问题是,如何从不包含导航控制器或 TabBarController 的故事板导航回 main.storyboard,它将包含选定索引为 1 的 TabBarController?

标签: swiftxcodefirebaseuiviewcontrolleruistoryboard

解决方案


当您展示 onbaording 时,您应该使用 with 返回选项卡

self.dismiss(animated:true,completion:nil)

对于复杂的演示,您可以这样做以便于重新返回

let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController  
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = vc

更好的是

  let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController 
  let nav = UINavigationController(rootViewController:goTo)
  nav.isNavigationBarHidden = true
  self.present(nav, animated: true, completion: nil)

onbarding 内的流程应该使用 pushViewController

然后在最后一个 onbaording vc 中解雇

  if let tab =  (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController {
     tab.dismiss(animated:true,completion:nil)
  } 

推荐阅读