Thomi Thomi - 1 year ago 301
Swift Question

iOS revealViewController() always returns NIL

I face some troubles with revealViewController.
Xcode 7.2, ios 9.2

My app starts with a first 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

Now 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.revealViewController()
self.menuButton.action = "revealToggle:"

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

Could you please help me to fix this issue?
I think I lost the revealViewController somewhere (maybe when I jump from the first Navigation Controller to the second) but I do not know how to do.

Thanks a lot.



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