简体   繁体   中英

How to open a TabBarController in ViewControllers that Instantiate from SlideMenuViewController

I want to have tabBar in all my ViewControllers. I have implemented the SWRevealViewController, which have two view controllers linked one is TabBarController and another is TableViewController I wants to have the same tabBar in all my ViewControllers that Segues from TableViewController.

在此输入图像描述

I want to have tabBar in all my ViewControllers. I have implemented the SWRevealViewController, which have two view controllers linked one is TabBarController and another is TableViewController I wants to have the same tabBar in all my ViewControllers that Segues from TableViewController.

You can do some tweak like this, create the custom delegate and set delegate of TableViewController and TabBarController to SWRevealViewController . Now first time lets say you open the TableViewController and when tap on any cell then just invoke the delegate method which should execute inside SWRevealViewController class and then perform segue inside that class which should open the TabBarController and when click back button of TabBarController again invoke the delegate method which should execute inside SWRevealViewController class and perform segue to open the TableViewController

This shouldn't be too difficult. Your RevealViewController (the initial controller of your storyboard) already seems to be a subclass of SWRevealViewController . This is good, but it's not essentially needed for this scenario. What you do need is a UIViewController implementation for your side menu. The Storyboard alone won't do.

Also, I wouldn't use segues here to switch between the tabs because a segue has a destination view controller. So every time you perform a segue, you would create a new instance of the destination view controller. I would rather implement a UIViewController that handles the cell selection of the UITableView

Now let's say you create a UIViewController or UITableViewController as your menu (actually the RearViewController ). Make sure to assign this class to your UITableViewController in your storyboard.

class MenuViewController: UITableViewController {
    // MARK: UITableViewDelegate
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let tabIndex = indexPath.row
        let tabbarVC = (revealViewController().frontViewController as! UITabBarController)

        tabbarVC.selectedIndex = tabIndex // this assumes the menu items are in the same order as the tabs. if not you need to adjust this

        revealViewController().revealToggle(animated: true) // close the side menu after switching tabs
    }
}

this should be all you need

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