programatically loading controllers using SWRevealViewController and Swift

883 views Asked by At

I am using SWRevealViewController in Swift and have built a dynamic UITableView for the left side navigation to load menu options. Based on the didSelectRowAtIndexPath that the user selects, I then attempt to load the new ViewController using

self.revealViewController().pushFrontViewController(<<name_of_new_view_controller>>(), animated: true)

In the new view controller, under viewDidLoad(), I have the following code:

if self.revealViewController() != nil {
    menuButton.target = self.revealViewController()
    menuButton.action = "revealToggle:"
}

When this view loads however, I get an error:

fatal error: unexpectedly found nil while unwrapping an Optional value

The item in question that is nil, is actually the menuButton which is a Bar Button Item (it's the hamburger button to show/hide the side navigation menu).

Any ideas why this wouldn't be working? It is all linked as an outlet from my storyboard to my class.

1

There are 1 answers

0
Mukund Agarwal On

I ran in to the same error while implementing this.
I missed the step of connecting the menuButton as an outlet to the ViewController by Ctrl dragging over which results in

@IBOutlet weak var menuButton: UIBarButtonItem!

I hope this is the same issue causing your error.

You can also use the following tutorial: https://www.youtube.com/watch?v=8EFfPT3UeWs