williej926
williej926

Reputation: 123

Self.revealViewController() returns nil

I am currently trying to get the pan gesture to reveal the controller. As of right now, my open button works, but when I add the last line, my program crashes because revealViewController is nil. Link to pod: https://github.com/John-Lluch/SWRevealViewController.

    import Foundation
    import UIKit
    class MainViewController : UIViewController{
    @IBOutlet weak var mainButton: UIView!
    @IBOutlet weak var Open: UIBarButtonItem!
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        mainButton.makeCircle();
        Open.target = self.revealViewController()
        Open.action = #selector(SWRevealViewController.revealToggle(_:));
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());
    }
}

Upvotes: 1

Views: 1275

Answers (2)

Chris Sprague
Chris Sprague

Reputation: 368

I was just having this same issue and came across this post. My issue turned out to be more of an "oops!" as I missed something obvious and spent 30 minutes scratching my head on it. :facepalm:

If the accepted answer doesn't work for you, check to make sure you connected your menu property to the storyboard with an outlet.

Swift:

@IBOutlet weak var menuButton:UIBarButtonItem!

override func viewDidLoad() {
    if (self.revealViewController() != nil) {
        menuButton.target = self.revealViewController()
        menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
    }
}

Upvotes: 0

Lawliet
Lawliet

Reputation: 3499

It's likely you haven't set your SWRevealViewController to be the rootViewController. Remember to set Custom Class and Storyboard ID to be SWRevealViewController in your Identity inspector.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")    
    self.view.window?.rootViewController = rootVC

    // Cont. with your code...
}

In case you need to show a login page at the beginning, you need a function in you AppDelegate, which allows the window to update the rootViewController

func changeRootVCToSWRevealVC () {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
    if let window = self.window{
        window.rootViewController = rootVC
    }
}

After user logged in, you can change the rooViewController to SWRevealViewController.

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
    appDelegate.changeRootVCToSWRevealVC()
}

If you have a variable storing the state of the last log-in, and want to navigate directly to the Navigation Drawer menu rather than LoginViewController.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let rootVCId = isLoggedin ? "SWRevealViewController" : "LoginViewController"
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: rootVCId)    
    self.view.window?.rootViewController = rootVC

    // Cont. with your code...
}

Upvotes: 3

Related Questions