0

This is my StartViewController with simple @IBAction:

class StartViewController: UIViewController {

    @IBAction func startButtonTapped(sender: UIButton) {
        revealViewController().revealToggleAnimated(true) //error
    }
}

Along with the commented line there is an error:

fatal error: unexpectedly found nil while unwrapping an Optional value

What am I doing wrong?

Of course StartViewController is presented from SWRevealViewController via sw_front segue.

For some reason revealViewController() returns nil here. Why?

This is how my storyboard looks like:

enter image description here

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

2 Answers2

2

When you implementing SWRevealViewController using storyboard, you should set Storyboard Entry Point to SWRevealViewController instance (Reveal View Controller on picture), not to front view controller (Start View Controller on picture).

Note that revealViewController method return optional pointer, and even when all is set properly it return nil until view is loaded, so you better use optional chaining:

revealViewController()?.revealToggleAnimated(true)
Yury
  • 6,044
  • 3
  • 19
  • 41
0

I think your app might crash because you are missing a delegate.Try to add the following to your protocols:

class StartViewController: UIViewController,SWRevealViewControllerDelegate {

    @IBAction func startButtonTapped(sender: UIButton) {
        revealViewController().revealToggleAnimated(true) //error
    }
}

If it still crashes, try changing the revealViewController().revealToggleAnimated(true) with revealViewController().revealToggle(self) and see if it still crashes.

SoundShock
  • 485
  • 1
  • 3
  • 24