Thomi
Thomi

Reputation: 649

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.

Upvotes: 5

Views: 4294

Answers (3)

webjunkie
webjunkie

Reputation: 1184

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)

Upvotes: 3

Holmes
Holmes

Reputation: 517

Incase you are skipping login scene based on the current user information, then make sure to instantiate the storyboard with SWRevealViewController. See below code for reference:

if User.currentUser != nil {
        //There is a current user
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController")
        window?.rootViewController = vc
    }
    else{
        //No current user
    }

Upvotes: 0

Ismail
Ismail

Reputation: 2818

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

Upvotes: 4

Related Questions