Вопрос

What I'm doing is creating a login screen as the first page. Once you login and it verifies you against the server it clears the login fields and it segues to a home screen. That screen has a back button that I've given the text logout. Clicking that takes you back to the login screen, and since the login button verifies you against the server before the segue it essentially looks like you're logging out.

However, I would like a logout button on each page. My thought for this was to add a button to the navigation bar of the other screens. As a test I added a logout button to a screen several levels in and added a segue back to the login screen. I updated a label on the page to see if it went back to the same page. When it segued back to the login screen the label was blank leading me to believe I didn't go back to the login screen, but rather it created a new login screen. How do I log out and have it essentially go back to the beginning?

Am I going about this the right way or is there a best practice in regards to this?

Это было полезно?

Решение

Every time to transition to a new view controller via a segue you are creating a new instance of the destination view controller. So yes, if you go back to the login screen using a segue you will be adding more view controllers to the navigation stack.

It sounds like you are using a UINavigationController. If so you can use the method popToRootViewControllerAnimated: to remove all view controllers from the navigation stack and return to the root (which is your login view controller). The other view controllers will be dealloc'ed when they are removed from the nav stack and you won't have the eventual memory problem you describe.

// do this when the user clicks your Logout button
[[self navigationController] popToRootViewControllerAnimated:NO];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top