revealViewController() always returns nil

Thomi

I'm having some troubles with revealViewController 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 SWRealViewController.

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

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

But self.revealViewController() always returns nil, so it does not work.

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

Ismail

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

NSClassFromString() always returns nil

PKPaymentAuthorizationViewController always returns nil?

A Clojure function that always returns nil

NSDateFormatter dateFromString Always Returns nil

Swift - findObjectsInBackgroundWithBlock always returns nil

+[NSManagedObjectModel mergedModelFromBundles::forStoreMetadata:] always returns nil

objc_getAssociatedObject always returns nil

viewWithTag always returns nil in child view controller

Swift: IBoutlet always returns nil object?

Swift date from string always returns nil

Why JSONDecoder always returns nil for an optional property?

reading from .plist always returns nil

PFTwitterUtils logInWithBlock always returns nil for userId

Swift String to Int Always Returns nil

DateFormatter dateFromString Always Returns nil in swift 3

CLGeocoder always returns nil for latitude / longitude

Swift NSFetchedResultsController Sections Always returns nil

'[Bool]' to 'nil' always returns true - Issue in SwiftUI

recursive lua function always returns nil

requestImageForAsset always returns nil - even after checking for nil

Swift: Calendar nextDate always returns nil if trying to match year component

Command line application xcode with core data pathForResourse always returns nil

UIColor(named:) always returns nil on iOS 11.0-11.2

Converting String To NSDate Returned From Google Always Returns Nil

Swift 3 / iOS 10 / TodayExtension - UserDefaults always returns nil

PNG/JPEG representation from CIImage always returns nil

URL to local Bundle.main always returns nil

My recursive function to search for an element always returns nil

self.navigationController always returns nil using storyboard to storyboard navigation