0

我在以編程方式打開新的View Controller時遇到此問題。選項卡欄不顯示在導航控制器內的視圖控制器上

let controller = self.storyboard?.instantiateViewController(withIdentifier: "overViewScreen") as! OverviewViewController 
controller.user = self.userObject 
let navigationController = UINavigationController(rootViewController: controller) 
self.present(navigationController, animated: true, completion: nil) 

我的項目結構: storyboard

在我的故事板標籤欄顯示到視圖控制器(上右表),但是當我運行的應用程序,它看起來像這樣: enter image description here

我希望你們能幫助我! 謝謝。

回答

0

您正在介紹無控制器欄的NavigationController。你需要提供TabBarController。

給你TabBarController標識符和實例化它們就像你用控制器

代碼註釋完成:

let tabVC = UIStoryboard(name: "NameOfYourStoryboard", bundle: Bundle.main).instantiateInitialViewController() as! UITabBarController 

let navVc = tabVC.viewControllers.first as! UINavigationController 

let vc = navVc.viewControllers.first as! OverviewViewController 
vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint 
vc.user = self.userObject 

present(navController, animated: true, completion: nil) 
+0

你的意思是這樣嗎?讓tabBarController = self.storyboard?.instantiateViewController(withIdentifier:「TabBarController」) self.present(tabBarController!,animated:true,completion:nil) –

+0

或類似的東西: let tabVC = UIStoryboard(name:「NameOfYourStoryboard」, bundle:Bundle.main).instantiateInitialViewController()as! UITabBarController let navVc = tabVC.viewControllers.first as! UINavigationController let vc = navVc.viewControllers.first as! LoginViewController vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint vc.user = self.userObject 存在(navController,動畫:真,完成:無) –

+0

所以現在我有:讓tabBarController = self.storyboard .instantiateInitialViewController()的?! UITabBarController 讓navVc = tabBarController.childViewControllers.first as! UINavigationController let vc = navVc.childViewControllers.first as! OverviewViewController vc.user = self.userObject self.present(navVc,animated:true,completion:nil)。錯誤:無法將LoginViewController投射到UITabBarController –

0

好吧,我設法解決這個問題是這樣的:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController 
          vc.user = self.userObject 
          let nvc = UINavigationController(rootViewController: vc) 
          self.present(nvc, animated: true, completion: nil) 

我做了一個單獨的控制器類「TabBarController」,併爲此類添加了一個屬性「user」。在我的「OverViewController」中,我可以得到以下屬性:

let tabBar: TabBarController = tabBarController as! TabBarController 
    user = tabBar.user 

感謝您的幫助!

相關問題