使用选定的 TableView 项和选项卡栏控制器显示视图控制器

Show View Controller with Selected TableView Item and Tab Bar Controller

我正在尝试从我的 Main.storyboard 文件中显示一个 SharkProfileTableViewController,并在 Upload.storyboard 中没有选项卡的模态视图中从按钮操作中显示一个 TabBarViewController酒吧.

我要展示的 ViewController 是根据模式视图中的数据从 SharksTableViewController 表格视图中选择的项目。

如何显示 SharkProfileViewControllerTabBarViewController

@IBAction func viewProfileButtonPressed(_ sender: UIButton) {
    let stb = UIStoryboard(name: "Main", bundle: nil)
    let tabBar = stb.instantiateViewController(withIdentifier: "tabBar") as! TabBarViewController
    let sharkTabBar = stb.instantiateViewController(withIdentifier: "sharkTableView") as! SharksTableViewController
    let sharkProfile = stb.instantiateViewController(withIdentifier: "sharkProfile") as! SharkProfileTableViewController

    sharkProfile.selectedShark = shark as JSONObject

    tabBar.selectedIndex = 3

    self.present(tabBar, animated: true) {

    }

}

TabBarController - 'Shark' 选项卡是应该显示的选项卡

SharksTableViewController - 在此表格视图中选择一个项目时,它会显示...

SharkProfileTableViewController - 这是我要呈现的视图(显示标签栏)

如果您想将您在 UITabbarController 中添加的任何数据传递给 SharkProfileTableViewController,那么您可以使用 UITabbarControllerviewControllers 属性 访问它]. viewControllers 属性 将 return UIViewController 数组,因此您需要在 Tabbar 中使用带有控制器索引的下标来访问它。

@IBAction func viewProfileButtonPressed(_ sender: UIButton) {
    let stb = UIStoryboard(name: "Main", bundle: nil)
    let tabBar = stb.instantiateViewController(withIdentifier: "tabBar") as! TabBarViewController
    //If SharkProfileTableViewController at 4 position in tabbar then access it from array like this
    let nav = tabBar.viewcontrollers?[3] as! UINavigationController 
    let sharkProfile = stb.instantiateViewController(withIdentifier: "sharkProfile") as! SharkProfileTableViewController
    sharkProfile.selectedShark = shark as JSONObject        
    tabBar.selectedIndex = 3        
    self.present(tabBar, animated: true) {
        nav.pushViewController(sharkProfile, animated: false)
    }
}

现在您只需更改 viewcontrollers?[3] 中的索引即可访问数组中的其他控制器。