0

I have a dashboard, then a login screen and profile screen. Once I reach profile screen I want login screen to be removed so that when I press back button, I am taken to dashboard instead of login. All these are view controllers of a navigation view controller and I display tge using present method.

I have a senario where login can appear anywhere during using navigation. Example: Dashboard, Screen 2, Screen 3, Login, Profile. Here, I have to remove login and when user taps back from profile, it should display Screen 3.

johndoe
  • 1,431
  • 2
  • 9
  • 26
  • Hope this can help: [link](https://stackoverflow.com/questions/33520899/single-function-to-dismiss-all-open-view-controllers) – prex Feb 09 '19 at 16:05
  • In that answer all view controllers are dismissed. In my case I would then need to check if the view controller is login. How to do that? – johndoe Feb 09 '19 at 16:20
  • Okay how about this one? [link](https://stackoverflow.com/questions/41327216/dismiss-or-remove-previous-modally-presented-view-controller-as-soon-as-the-next) – prex Feb 09 '19 at 16:34

3 Answers3

0

If you are logged-in already and you have to move to top viewController you can do window.rootViewController.dismiss(animated: true, completion: nil)

But If you are using navigationController then this will work: navigationController.popToRootViewController(animated: true)

Rahim Khalid
  • 451
  • 5
  • 15
0

Implement the following in your login view controller:

override func viewWillDisappear(_ animated: Bool) {
    var navigationArray = navigationController?.viewControllers
    let count = navigationArray?.count
    navigationArray?.remove(at: count! - 2)
    navigationController?.viewControllers = navigationArray!
}

This removes the current view controller(the login VC in your case) from the navigation stack just when it is about to disappear. So when you tap the back button from the next VC it will always take you to the VC preceding the login.

lajosdeme
  • 1,535
  • 1
  • 5
  • 16
  • I edited my answer. My first answer wasn't working as supposed. I tested this solution and this works. – lajosdeme Feb 09 '19 at 17:03
  • Works perfectly. Thanks. I had added a check to remove the login screen only if `isLoggedIn` was success. – johndoe Feb 11 '19 at 06:59
0

This is generic answer, not for the answer of this question only.

When popping, you may kick out the viewControllers from your navigation Controller, would solve your problem

extension UINavigationController { 
     public func removeViewController(classes : [String]) {
           var vcs = [UIViewControllers]()
           for viewController in self.viewControllers {
               let name = viewController.className
               if !classes.contains(name) {
                    vcs.append(viewController)
                }
           }
           if classes.count < vcs.count {
               self.viewControllers = vcs
           }
    }
}

now think you have 3 viewControllers , dashboard, login, profile. you want to remove login and Move Back from profile to dashboard

In profile's View Controller

override func viewDidLoad() {
   super.viewDidLoad()
   //your works
   let viewControllersToRemove = [String(describing: type(of:login))]
   navigationController.removeViewController(classes : viewControllersToRemove)
}
Ankur Lahiry
  • 1,599
  • 1
  • 8
  • 17
  • I updated the code to `if vcs.count < classes.count { self.viewControllers = vcs } } }` and use it as `let viewControllersToRemove = [NSStringFromClass(type(of: this))] this.navigationController!.removeViewController(classes: viewControllersToRemove)` But I am getting view controller presenting on detached is discouraged and gets a blank screen. – johndoe Feb 11 '19 at 06:55