0

I am using navigation viewController for Swipping between viewControllers.
I have a logout button on a subview of navigation controller. How can i go back to login screen when clicked on logout button?

Note : .xib file contains only two place holders

  1. File's owner
  2. First Responder
surajs1n
  • 1,493
  • 6
  • 23
  • 34
myrri
  • 19
  • 6

3 Answers3

1

If your loginViewcontroller is rootviewcontrollerand and you have only oneNavigation controller` to reach particular view then in that particular view you can write on logout button's click,

   self.navigationController?.popToRootViewControllerAnimated(true)

If you have two NavigationController between to reach that view then,

 self.navigationController?.navigationController?.popToRootViewControllerAnimated(true)

Likewise you can handle multiple navcontroller also.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

Assuming the very first view in your navigation controller is the login view controller, you can do: navigationController!.popToRootViewControllerAnimated(false)

Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • Yes, login controller is initial view controller. but this line of code is not working for me. Any suggestion please? – myrri Jun 04 '16 at 06:49
0

If your Login controller is root controller then you can move like this

self.navigationController?.popToRootViewControllerAnimated(true)

else

//Move to login view
let viewControllers = self.navigationController?.viewControllers as NSArray!
for vc in viewControllers {
    if vc.isKindOfClass(LoginViewController.classForCoder()) {
        self.navigationController?.popToViewController(vc as! UIViewController, animated: false)
        break
    }
}
Pushpa Y
  • 1,010
  • 1
  • 12
  • 20
  • how can i make login viewController as root controller? – myrri Jun 04 '16 at 06:22
  • Make root view controller : http://stackoverflow.com/questions/15774003/changing-root-view-controller-of-a-ios-window, http://stackoverflow.com/questions/22653993/programatically-change-rootviewcontroller-of-storyboard – Pushpa Y Jun 04 '16 at 06:44