0

獲取的UITabBarController我從TabbarController上呈現的ViewController/NavigationController

let planSettingsVC = PlanSettingsViewController.instantiateFromStoryboard() 
let planInfoNavCon = UINavigationController(rootViewController: planSettingsVC) 

if let myTabBarVC = self.tabBarController as? MyPlanTabBarController { 
     myTabBarVC.present(planInfoNavCon, animated: true, completion: nil) 
} 

PlanInfoNavCon呈現導航控制器被提出,現在我想訪問它呈現的TabBarController。

發現在圖書館:

open var tabBarController: UITabBarController? { get } // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise. 

所以,我想對PlanSettingsViewController

  1. print(self.tabBarController)打印

  2. 以下個打印

  3. print(self.navigationController?.presentingViewController?.tabBarController)打印

  4. print(self.navigationController.tabBarController)打印

  5. self.navigationController?.presentingViewController as? MyPlanTabBarController結果

我能夠訪問TabBar的ViewController TabBarController self.tabBarController

如何訪問TabBarController提出了這個?

P.S.

po self.navigationController回報<UINavigationController>po self.navigationController?.presentingViewController回報<UIViewController>

lldb

回答

0

此代碼:

if let myTabBarVC = self.tabBarController as? MyPlanTabBarController { 
     myTabBarVC.present(planInfoNavCon, animated: true, completion: nil) 
} 

說: 「目前planInfoNavCon self.tabBarController」

好奇...它se EMS每個VC在NavController認爲自己通過呈現該呈現的NavController控制器...所以...

你仍然不需要額外的.tabBarController的一部分,但你也不需要得到參考導航控制器。

此代碼,在當前 VC在PlanSettingsViewController,應該得到適當的參考tabBarController:

var thePresenter = self.presentingViewController as? MyPlanTabBarController 

因此,要獲得一個參考tabBarController從planInfoNavCon,你想獲得它的主持人

var thePresenter = self.navigationController?.presentingViewController 

你不想額外.tabBarController部分(可能需要其他?展開 - 我實際上並沒有運行這個代碼)。

+0

我試過這個。 'self.navigationController?.presentingViewController as? MyPlanTabBarController'返回nil – sasquatch

+0

您是否在調試中檢查了值?你確定self.navigationController在那個時候是有效的嗎?如果你*不把它當作? MyPlanTabBarController','self.navigationController?.presentingViewController'的值/類是什麼? – DonMag

+0

是的,我處於調試模式。 'po self.navigationController'返回''和'po self.navigationController?.presentingViewController'返回''。當我嘗試將它轉換爲TabBarController – sasquatch