1

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

Rigsby
  • 180
  • 2
  • 14

1 Answers1

1

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)
gcharita
  • 7,729
  • 3
  • 20
  • 37
  • you mean, I have to change my code like this `@IBAction func clickButton(_ sender: Any) { let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! UITabBarController self.revealViewController()?.setFront(nextViewController, animated: true) }` But after changing this I am not able to navigate. – Rigsby Oct 27 '20 at 10:09
  • @Rigsby you are not able to navigate from the side menu or from the tabbar? – gcharita Oct 27 '20 at 10:14
  • I am clicking on that orange button from the first VC, on click of that, I am trying to navigate, clickButton is the method called on orange button click – Rigsby Oct 27 '20 at 10:15
  • @Rigsby oh, that supposed to present the `TabBarController` along with the side menu? – gcharita Oct 27 '20 at 10:18
  • @Rigsby sorry, I got it wrong. Then you need to present your `RevealViewController` instead of your `TabBarController`. (the one that is left of your `TabBarController`) Try to add a `Storyboard ID` to your `RevealViewController`, instantiate it and then present it. – gcharita Oct 27 '20 at 10:23