How can we dealloc the view that we are no longer use it any more? - iphone

I am currently design a simple application which the user should login first.
I set the login page as the root view of the window. Once the user login the system, it will modal present a tabbar view.
I guess the login pages (which are actually several view controllers in navigation controller) are useless after then. Can I dealloc those pages and reset the root view as the tabbar controller?

Don't set the Login page as the root. Presenting a modal view with a tab bar that should now act as the root is the wrong way, and most likely be rejected by Apple for this.
The second view you currently have, or the view the user should see AFTER logging in should be the root. In this root view, check to see if the user is logged in, if not, present the login as the modal view.

You should never call dealloc directly from your code. With no ARC, the only exception is calling dealloc for the super class. With ARC (and you should use ARC), you don't have to call dealloc anymore.
That being said, since the login screen is only going to be needed in a few runs, why don't invert the flow, and make the tabbar controller the root of your app, and only when it is needed present the login view, perhaps without animation, so the user will never see the root?

Related

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.

Closing a Modal (via delegate) from a different View

I'm hoping that i'm simply overlooking something simple. But here the scenario.
I have a root vc that opens a modal view LoginScreen. In that view class I've set up a protocol (ViewControllerDelegate) synthesized, and once a user logs in via the modal, the parent VC closes the modal and all is well.
In LoginScreen.m to close the modal all I have to use is this code:
[self.delegate loginConfirmed:self];
That works perfectly. However, I have another view that gets pushed is login is confirmed. Still as a modal of course. Call it LoginScreen2.
I can't figure out how to get the Modal to close from LoginScreen2 and get back tot he root VC. I've tried a bunch of different variations like this, but no luck.
[((LoginScreen *)self.parentViewController).delegate loginConfirmed:((LoginScreen *)self)];
I've been searching and messing around for a couple hours, with no luck. Again I'm hoping I'm just missing something simple and just not seeing it. If anyone has any ideas that would be awesome.
Not sure who is presenting the second view controller, but one thing that might help you is to know that a modally presented view controller can dismiss itself:
[self dismissModalViewControllerAnimated:YES];
The root view controller can check the user state when it appears, and presentModalViewController:Login1. Login1 changes the user sign-in state, and dismisses itself. Root sees different user state (when it's about to reappear via the viewWillAppear: method) and presents modal on Login2. No delegation needed.

Trying to understand segues, memory management, and best practices

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

Present login screen on app startup

In my previous question on UIView animations I mentioned that I have to simulate the modal behavior for some view. Here I'll explain why and ask about some better solution.
The first view a user should see in my app is the login screen. Upon successful authentication the program checks if the user is a parent or a child (in a family). For each of these roles the app must provide different functionality.
So I designed the app like this:
Declare outlets for the login view controller and a tab bar controller (for the main part of the interface) in my AppDelegate.
In the application:didFinishLaunchingWithOptions: method
Set the rootViewController of the main window to the login view controller.
Make AppDelegate the delegate for the login controller, so it can send notification when it's done its job.
When AppDelegate receives the message on successful login, it determines whether the user is a parent or a child, instantiates the set of view controllers that provide corresponding functionality and passes them to the tab bar controller.
At last AppDelegate switches the rootViewController of the main window to the tab bar controller.
Certainly the user can logout, then the rootViewController is switched back again to the login controller.
I would like to present and dismiss the login screen as if it is a modal view, but AppDelegate only has a bare window, thus I don't have an object to send presentModalViewController: to. This brings up a question:
First of all, is it a good design?
And if it is, how do I simulate a modal behavior correctly?
I think you're on the right track.
However, I always try to get out of the app delegate as soon as I can, leaving it only to do application-level things (like respond to notifications, go in and out of background). In this case, doing so will help you.
In the appDelegate, create a new UIViewController class, something like "startUpController".
Add it's view to the app window.
Then in your startUpController, do everything that you used to do in the app delegate (login, tab bar setup, etc.).
And now, since you're in a view controller, you can presentModalViewController to your hearts content.
Hii,
you should refer this
http://code.google.com/p/tweetero/
https://github.com/jbrien/WordPress-iPhone
Hope this helps!

ViewController data sharing

So I have two view controller that I load on the app delegate when the app loads. One is for a login page where I have a username and password and the other one is a UITabBarViewController. After the user login, I just remove the login view and therefore showing the UITabBarViewController. The problem is that in my UITabBarViewController, I need the username and password from the ViewController. How can I solve this?
ADDITIONAL INFO:
Here's basically what I want to do:
Application starts with an Logon on page: User ID, Password and Logon button
On clicking the logon button, after validating the credentials, we've to take the user to the next screen with
an Navigation Bar on the top (essentially an UINavigationController)
Table view
Tab Bar in the bottom
Now after logging in, I want all the ViewControllers in the UITabBarViewController to be able to get the username and password that the user enters in the first login screen.
I think the best design is to have the your controller for the tab bar present a modal view controller for login. The controller tab bar would then be the delegate of the LoginViewController and the LoginViewController would notify its delegate when the login is complete. When the login is completed successfully then the controller for the tab bar can dismiss the LoginViewController.
Now I wrote this code after you updated the answer. You can figure out how to add the UINavigation bar and table views your self. The question is about passing data between view controller not me answering how to through a bunch of views together. I highly recommend to iTunes U course from Stanford on iOS programming if you want to learn more about putting many views and controllers together for a complete app.
I provide a full, complete and working example demonstrating proper use of delegates to share data between the LoginViewController and a UIViewController (In your case the UIViewController would be replaced by your tab bar controller). I also demonstrate how to use NSUserDefaults to save this data which is accessible from elsewhere is the app.
All the code for the example can be found here.
1 Use root controller to share the data.
You can put the data in your root controller and the root controller creates two view.
The root controller can transfer the data to its child views.
2 Or use class constructor.
When you create the views, you can give them parameters with data.
That is not a big Problem.
You can create two NSString variables in the appDelegate class and set the property to them. So that you can access those variable in the entire project.
You will set the values in the login page and you will access those in the tabBarController.
Thats it.
Reagards,
Satya