【问题标题】:How can i return to Main.storyboard which contains a TabBarController from another Storyboard我怎样才能从另一个故事板返回包含 TabBarController 的 Main.storyboard
【发布时间】:2019-07-27 11:11:08
【问题描述】:

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

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

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

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

我也在其他 Storyboard 中尝试过,但使用此按钮不会改变视图:

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?

【问题讨论】:

    标签: swift xcode firebase uiviewcontroller uistoryboard


    【解决方案1】:

    当您展示 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)
      } 
    

    【讨论】:

    • 我试过这个,但是使用这行代码它只会关闭“入职”中的最后一个视图控制器,并在第二个故事板中显示第一个 VC
    • 太好了,现在完美运行,正是我想要的! :) 再次感谢 Sh_Khan 的出色帮助!
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2015-04-13
    • 2014-05-16
    • 2020-05-28
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多