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

时间:2017-06-22 09:37:18

标签: ios swift uitableview uitabbarcontroller uistoryboard

我试图在我的SharkProfileTableViewController文件中使用Main.storyboard的{​​{1}}来自TabBarViewController中没有标签的模式视图中的按钮操作杆

我尝试呈现的ViewController是基于模态视图中数据的Upload.storyboard tableview中的选定项目。

如何显示SharksTableViewController以及SharkProfileViewController

TabBarViewController

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

SharksTableViewController - 在此tableview中选择一个项目时,它会显示... SharksTableViewController

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

1 个答案:

答案 0 :(得分:1)

如果您想将任何数据传递到SharkProfileTableViewController中已添加的UITabbarController,那么您可以使用viewControllers UITabbarController属性访问该数据。 viewControllers属性将返回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]中的索引即可从阵列访问其他控制器。

相关问题