SWRevealViewController Open second view controller from menu programatically using swift

169 Views Asked by At

I am using SWRevealViewController in my app. Everything is fine except one thing.

Suppose there are A,B,C 3 items in my SWRevealViewController Menu. I want to open B item programatically using swift.

UPDATE: CODE

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let sw = storyboard.instantiateViewController(withIdentifier: "Reveal") as! SWRevealViewController
        let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
        let navigationController = UINavigationController(rootViewController: destinationController)
        sw.pushFrontViewController(navigationController, animated: true)
2

There are 2 best solutions below

1
gcharita On

You can have access to the main screen controller using frontViewController property of the SWRevealViewController like that:

let navigationController = revealViewController().frontViewController as? UINavigationController
navigationController?.pushViewController(viewController, animated: false)

You can replace the frontViewController using setFront(_:animated:) function of SWRevealViewController like:

revealViewController()?.setFront(viewController, animated: false)
2
Mohammad Aamir On

This is complete solution two my question.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
        let navigationController = revealViewController().frontViewController as? UINavigationController
        navigationController?.pushViewController(destinationController, animated: false)