Sajeel
Sajeel

Reputation: 168

iOS Swift always start with same view controller

enter image description here

I am facing 2 problems;

  1. How to present the login screen always means even the app comes from background I need to show the login page.

  2. I have a logout button in Home controller, I moved from login to home using Show Segue, but I am unable to move back to the login on clicking of logout. I used the below code for moving to login page

    self.navigationController?.popViewControllerAnimated(true)
    

Upvotes: 0

Views: 486

Answers (2)

Oshadha
Oshadha

Reputation: 548

You can use a segue in the matter and on the button click you can use following code.

self.performSegueWithIdentifier("logoutSegue", sender: self)

Please note that I have give the segue name as "logoutSegue".

Upvotes: 0

nhgrif
nhgrif

Reputation: 62062

Your login page is not inside a navigation controller. Your navigation controller can not pop to it. Instead, we need to dismiss the navigation controller itself.

self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

So, call this code any time you want to get back to that first login page. When doing it when the app has come back from the background, tap into the application delegate's applicationWillEnterForeground(application:) method.

Given that we likely won't have a reference to the navigation controller or any of our view controllers from the app delegate, we can instead just have the app delegate's window's root view controller (which should be your login controller) dismiss everything. So add this in applicationWillEnterForeground(application:):

self.window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil)

Upvotes: 1

Related Questions