简体   繁体   中英

Show View Controller with Selected TableView Item and Tab Bar Controller

I'm trying to present a SharkProfileTableViewController from my Main.storyboard file with a TabBarViewController from a button action in a modal view in Upload.storyboard that has no tab bar.

The ViewController I'm trying to present is a selected item from a SharksTableViewController tableview based on the data from the modal view.

How do I show the SharkProfileViewController along with the TabBarViewController ?

@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 - the 'Shark' tab is the tab that should show TabBarController

SharksTableViewController - when selecting an item in this tableview it presents the ... SharksTableViewController

SharkProfileTableViewController - this is the view I'm trying to present (with the tab bar showing) SharkProfileTableViewController

If you want to pass any data to SharkProfileTableViewController that you have added in UITabbarController then you can access it using the viewControllers property of UITabbarController . viewControllers property will return array of UIViewController so you need to access it using subscript with your controller index in 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)
    }
}

Now you need to simply change index in viewcontrollers?[3] to access other controller from array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM