Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

revealViewController() always returns nil

Tags:

ios

swift

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.

like image 883
Thomi Avatar asked Jan 25 '16 07:01

Thomi


2 Answers

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

like image 141
Ismail Avatar answered Sep 24 '22 07:09

Ismail


In case someone is wondering how to do a manual segue, this is what worked for me at the end.

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let sw = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! SWRevealViewController

    self.view.window?.rootViewController = sw

    let destinationController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardID") as! NameOfViewController

    let navigationController = UINavigationController(rootViewController: destinationController)

    sw.pushFrontViewController(navigationController, animated: true)
like image 37
webjunkie Avatar answered Sep 23 '22 07:09

webjunkie