Thomi Thomi - 2 years ago 156
iOS Question

revealViewController() always returns nil

I'm having some troubles with

in Xcode 7.2 and iOS 9.2.

My app starts with a view controller embedded in a navigation controller to perform a login.
After login, or if the login token is present, I jump to another view controller embedded in a navigation controller with the following code:

let homePage = self.storyboard?.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let homePageNav = UINavigationController(rootViewController: homePage)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = homePageNav

In this home view controller I would like to have a left navigation menu with

I had the
view linked with
to my home navigation controller, and the following code:

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

always returns nil, so it does not work.

I think I lost the
somewhere (maybe when I jump from the first navigation controller to the second) but I do not know what to do.

Answer Source

The most convenient to be a reason for the revealViewController to be nil is you didn't connect segues correctly in stroyboard.

See this tutorial it's quite easy to follow.

Update If in your case you just need to open a login vc if the user is not logged in you may do like this:

in AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

   var rootVCStoryboardId = userIsLoggedin ? "SWRevealViewController" : "LoginViewController"
   self.window?.rootViewController = UIStoryboard(name: Storyboards.main, bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier(rootVCStoryboardId)

Where SWRevealViewController is the stroyboard id for SWRevealViewController and LoginViewController is the storyboard id for your login view controller(or its navigation controller if exists).

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download