Thomi Thomi - 6 months ago 16
iOS Question

revealViewController() always returns nil

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.

Answer

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

Comments