Nathan Barreto
Nathan Barreto

Reputation: 375

RevealViewController not working after programmatically set a view in iOS / Swift

First of all, let me introduce what I'm trying to do. Second... I'm new to Swift/iOS development.

What I'm trying to do is: When I open the APP, it loads the LoginVC. When the user logs in, the TableSelectionVC loads. If the user selects a cell, it goes to the Home screen with the selected values. If the user taps on the bell (blue arrows) the app should go to AlarmVC.

It should work, but it doesn't. The AlarmVC says that self.revealViewController() is nil. BUT If I go to alarmVC through the menu option (red arrows) it loads normally and the Menu is shown. Also, if I choose the option in green, it goes to the TableSelectionVC and if I tap the icon, it goes to alarmVC and it doesn't crash. The problem is if I go from LoginVC to TableSelectionVC and tap on the icon, the it will crash.

I think it is the way that I'm setting the views, instantiating a controller and setting the window.rootViewController.

In my AppDelegate I have the following functions:

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

func changeRootViewControllerToPlantSelectionVC () {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "navPlantSelectionVC")
    controller.modalTransitionStyle = .flipHorizontal
    if let window = self.window {
        window.rootViewController = controller
    }
}

When the user logs in the app, the following function is executed:

    static func goToPlantSelection() {
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        //there should be an alert error
        return
    }
    appDelegate.changeRootViewControllerToPlantSelectionVC()
}

After that, the PlantSelectionVC is shown and if the user taps on an cell the appDelegate.changeRootViewControllerToSWRevealViewController() is executed and the HomeVC is shown.

If the user taps on the icon, it tries to go to the AlarmVC but it crashes, like I said. I think I'm doing something wrong with binding the SWRevealViewController with LoginVC and TableSelectionVC.

The following code in AlarmVC tries to execute:

    static func setupMenuToggle(button: UIBarButtonItem, viewController: UIViewController) {
    button.target = viewController.revealViewController()
    viewController.revealViewController().rearViewRevealWidth = viewController.view.bounds.size.width * 0.9
    button.action = #selector(SWRevealViewController.revealToggle(_:))
}

But is shows the error in the third line: found nil while unwrapping an Optional value Can anyone help ?

Upvotes: 1

Views: 741

Answers (1)

Nathan Barreto
Nathan Barreto

Reputation: 375

I fixed it. I set the login screen as sw_front for the swRevealViewController and after that, when the user logs in I would change the swRevealViewController front view controller with

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      let controller = storyboard.instantiateViewController(withIdentifier: "navPlantSelectionVC")
      self.revealViewController().setFront(controller, animated: true)

It is working now.

Upvotes: 1

Related Questions