Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

SWRevealViewController - reveal it manually under @IBAction

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

Upvotes: 0

Views: 254

Answers (2)

Yury
Yury

Reputation: 6114

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)

Upvotes: 2

SoundShock
SoundShock

Reputation: 485

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.

Upvotes: 0

Related Questions