iosswiftswift4swrevealviewcontroller

swrevealcontroller not working after navigation to homepage


I am new to Swift and I am trying to implement side navigation using SWrevealcontroller

I have a root VC which has a button when click on this button I am navigating to UITabBarController

Here is how my storyboard looks like (below)

enter image description here

and this is how I am navigating on button click

@IBAction func clickButton(_ sender: Any) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! UITabBarController
    self.present(nextViewController, animated:true, completion:nil)
}

This is my code in FirstViewController

override func viewDidLoad() {
        super.viewDidLoad()
        barButton.target = self.revealViewController()
        barButton.action = #selector(SWRevealViewController.revealToggle(_:))
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

But I am getting error at line when I navigate from root VC to UITabBarController

self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

Fatal error: Unexpectedly found nil while unwrapping an Optional value

Can someone help me what is wrong here.

TIA


Solution

  • You need to present your RevealViewController instead of the UITabBarController.

    Add a Storyboard ID to your RevealViewController:

    enter image description here

    And then present it in your clickButton(_:) function, like this:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "RevealViewControllerID")
    self.present(nextViewController, animated: true)