swift - self.tabBarController?.selectedIndex 并在另一个 View Controller 中执行功能

标签 swift

我在选项卡栏 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 获取包含TutorViewControllerUINavigationController。并获取导航 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/

相关文章:

swift - 选择表格单元格时, View 上的frame.origin更改错误

swift - 在 RealityKit 中启用手势

ios - 保持任务在 View Controller 之间运行

ios - Google Maps SDK iOS 以编程方式更改楼层

swift - 使用 Apple 的 AVFoundationExporter 示例代码将 AIFF 导出到 AAC 让我有些问题

ios - chartValueSelected 永远不会被调用

ios - 突出显示并滚动到 UICollectionView 中的随机单元格

javascript - 在 Swift 4+ 中使用 WKWebView 和 Regex 执行/修改 Javascript 后获取 HTML

swift - 如何比较 Swift String.Index?

ios - 如何清除 iOS 中 UIWebViews 的 localStorage