Trying to understand segues, memory management, and best practices - ios5

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];

Related

Swift Facebook Login no url after logout

I've implemented this in my app: https://developers.facebook.com/docs/swift/login/
I've pretty much followed this to a tee, except that I have two view controllers with this implementation; one outside of a navigation controller for login (1), one inside of a navigation controller for logout (2).
I can login initially just fine, but after logout, going back to the view controller (1), after clicking "Continue with facebook", it just shows me a blank safari page.
When logging in for the first time, the url bar will show "facebook.com"
But after logout and when I want to re-login again, the url is just blank
May I know what I need to do? Thanks.
VC Code here: https://codedump.io/share/j1rxrJVMBw00/1
Edit: Login will open a navigation controller (indirectly), and logout will dismiss it. It seems like this is a cause of the problem but I'm not sure how.

Is it okay to use a Modal segue to leave a view that won't ever show up again?

I am working on an app that, when you first launch it, has a login screen that connects to a server. If you enter a valid username and password, this login screen goes away and you are taken to the main screen for this app.
The main screen gives you options of what to do, and it is assumed that you will not go back to the login screen again probably. Maybe 1% of people might log out of the app and go back to the login screen I guess.
Since I am not using a Navigation Controller to go from the login view to the main view, I have to use a modal segue, but it doesn't feel right to do so, as far as my knowledge of the theory behind modal segues. Am I crazy? Should I maybe just somehow use a Navigation Controller anyway?
I just feel this is not proper style, to use a modal segue to basically cover up the login screen, when it really isn't needed anymore.
You should have a home screen (main view controller) that displays the login screen modally on app start. Once login is complete you can dismiss the login screen and your app is running as usual.
It's extremely bad practice to leave a view controller running when it's unlikely to be used again.

IOS: 'Register/Login' view controller and going back to it as necessary

I'm having issues with a Registration/Login View Controller when the user manually wants to Logout of the app.
So, the very first VC of the app is as follows:
REGISTRATION/LOGIN VC : Checks if credentials already exist in the device, if so, automatically attempts to login and pushes to the HOME VC if successful. If there are no credentials then it displays a Registration form in the same VC.
Now, because I don't want people to be able to go back to the Registration/Login VC on their own once they are at the HOME VC, I remove all previous VC's from the navigation array upon loading the HOME VC. (Could this be part of the problem?)
Now, once logged in, users can pretty much at any time deregister and it should bring them to the Registration/Login VC. I do this by performing a segue from the VC with the logout button.
The problem is that when this segue goes through, I get a black screen with only the Navigation Bar on top. While debugging, I can see that the Registration/Login VC's logic does get executed (it checks that the user has credentials, doesn't find them and theoretically makes the registration form visible), but it is all still just black.
I'm not sure what is causing this but it has made me wonder really what is the best approach to having a Register/login VC as root but at the same time making it not be accessible through back-navigation.
Thank you for any help!

Logout issue in TabBar based app

This is quite a common question, but after trying a lot to fix the issue, finally I have decided to post it on StackOverFlow.com
I have a tab bar based app. The tab bar is loaded in AppDelegate.m as follows:
self.tabBarController.viewControllers = #[viewController1, viewController2 , viewController3 , viewController4 , viewController5];
My 5th tab has a button for logout. When user clicks logout, I want to clear/reset entire app and go to login page which is a modalviewcontroller.
I have tried following while logging out:
NSMutableArray * vcs = [NSMutableArray
arrayWithArray:[self.tabBarController viewControllers]];
[vcs removeAllObjects ];//ObjectAtIndex:4];
[self.tabBarController setViewControllers:vcs];
This removes all views from tab-bar. But when I login again, nothing is displayed. I want to show my home screen, i.e. tab item 1 selected by default.
I have read that its not a good practice to call didFinishLaunchingWithOptions again manually.
Is there a way where I can reset all tab-bars and reinitialise them again ?
This will help me solving one more problem that is linked with this situation. When user logs out and log in again, and view controllers are not cleared, then logout page is shown again after login. And not the home view controller.
Please help.
Thanks in advance.
If you really want to start over, you should put a method, lets call it -(void)setupTabBarController, in the app delegate, and at start up you would call it from application:didFinishLaunchingWithOptions:. Later when you want to reset, call that method again from the login page. This method would have the creation of all the tab bar controller's view controllers in it, as well as setting the tab bar controller as the root view controller of the window.
However, it's not really clear that you need to do this, depending on what state all those controllers are in at logout time. Your problem with the logout page being shown again could probably be fixed in a simpler way.
Well, nothing is showing because you removed the views and never added them back in.
There is no need to remove the view controllers from the tab bar after you log out. You can just write a method to reset all the data in each view controller and then set the selected tab to what you desire.
I know, this is not really an answer to your question, but this could maybe help you too (and as I can't comment on post yet i have to post it like this :)).
I had some problems with "resetting" the navigation stack when the user logs out in my tabbar app too. In the beginning I had my tabbar-controller as the root controller and was displaying the login-screen modally but than it was quiet hard reset the navigation stack once the user loged out.
What I ended up doing and it works for me quiet well is, I set the login controller as root controller and after log in displayed the tab navigation modally. On log out I simply dismiss the tabbar-controller again - everything starts from the beginning again.
Maybe you could try this and see if it is easier to handle.
You should be add tabBar controller on second view controller. main view controller show home screen. when you navigate second view controller then you add tabBar here.

Need an ideal way to handle tab bar in app

I want to create an app where there is a login screen, and when user logs in open up home page with tab bar controller.
I have created a single view application and have created login screen, home screen and a tab bar on it.
In tab bar delegate, I am adding screens to tab bar by [self.view addSubView firstView.view]
My problem is if I open up 4th tab, and switch to 2nd tab and I have to go back to home screen, so I press home btw on nav bar, it shows view 4 which is already on stack of uiviews.
How do I switch between all tab bar views and add navigation to them ? Which ideal method is to be used?
I've created app that is doing what you need (I guess). You can find it in app store for iphone using name Torchoo. It is free and there is test account so you can see how it works and if its really what you need. If yes, ping me here I will show you the sources and how I did it.
In couple of words, you need normal tabbar controller that has number of navigation controllers and each of them has number of view controllers. And login screen is just a modal view that shows/hides when you need it. Most of things may be done in storyboard.
I don't get it actually but i think you want to attach all the view by navigation controller.
But it's useless. if you want to use navigation bar then it ll navigate in another view but tabbar will give you different views on every tab.
so, first see the use of both of them then ask....