var messageViewController: MessageViewController?
var settingsViewController: SettingsViewController?
var otherViewController: OtherViewController?
override func viewDidLoad() {
   messageViewController = //the first one
   settingsViewController = //the second one
   otherViewController = //the third one
}

然后在代码中,我可以在需要时调用这些控制器中的方法。
messageViewController.reloadData()

最佳答案

viewControllersUITabBarController属性是对每个内容视图控制器的引用数组。如果您确切知道每个索引处的控制器,则可以从此数组中分配变量:

override func viewDidLoad() {
    super.viewDidLoad()
    messageViewController = viewControllers![0] as! MessageViewController
    settingsViewController = viewControllers![1] as! SettingsViewController
    otherViewController = viewControllers![2] as! OtherViewController
}

甚至使变量具有计算属性:
var messageViewController: MessageViewController { return viewControllers![0] as! MessageViewController }
var settingsViewController: SettingsViewController { return viewControllers![1] as! SettingsViewController }
var otherViewController: OtherViewController { return viewControllers![2] as! OtherViewController }

09-27 22:50