我在选项卡栏 Controller 中有 4 个 View Controller ,每个 View Controller 都嵌入了导航 Controller 。问题是,我已经研究并找到了在选项卡栏中从 Viewcontroller0 导航到 Viewcontroller 1 的最佳方法是使用 self.tabBarController?.selectedIndex = 1
,它工作正常。但是,我希望在导航后立即执行该功能。以下是我的尝试,但没有奏效。有人知道吗?非常感谢。
@IBAction func GOGOBut(_ sender: Any) {
self.tabBarController?.selectedIndex = 1
if let VC = presentingViewController as? TutorViewController {
VC.observeBBCases()
}
//NotificationCenter.default.post(name: NSNotification.Name(rawValue: "loadBAFS"), object: nil)
}
observeBBCases() 是 TutorViewController 中的函数 而 self.tabBarController?.selectedIndex = 1 就是去 TutorViewController
最佳答案
首先使用索引1
获取包含TutorViewController
的UINavigationController
。并获取导航 Controller 的 Root View Controller TutorViewController
。然后你可以调用方法observeBBCases
@IBAction func GOGOBut(_ sender: Any) {
if let navigationController = self.tabBarController?.viewControllers?[1] as? UINavigationController,
let vc = navigationController.viewControllers.first as? TutorViewController {
vc.observeBBCases()
self.tabBarController?.selectedIndex = 1
}
}
关于swift - self.tabBarController?.selectedIndex 并在另一个 View Controller 中执行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56404026/