I have an app where once the user logs in, they stay logged in until logged out. once logged in, some style aspects like 'name' are presented for the user to see. My problem is that when I am logged in as 'user1' and then I log out and log in as 'user2' the view still presents 'user1' to the user. This is because the text containing 'user1' is set in viewdidload() on my TabBarController and when I logout/in, viewDidLoad() is not called again so the text remains the same. Is there some function or way to call viewDidLoad() on all of my ViewControllers once I Login again?
I tried ViewDidAppear(…) but I don't really want to be calling to the database to receive the name every time the user clicks the VC
i think you are going to login . page from tabbar navigationcontroller . loginpage should not be in that hierarchy .
1 . you can still make it possible by using viewwillappear
2. we can make it in view didload and this is recommended approach .
make separate navigation controller for loginpage and set the window rootviewcontroller as per required business flow . so that once u log out ,change the wiindow,s rootviewcontroller so that entire tabbar controller will be removed from memory and then u login , change the root view controller to tabbarcontroller , now viewdidload will be called
You can use NotificationCenter and send custom notification "login".
If some object (for example VC) depend User, that VC need subscribe this notification and update state.
Documentation
https://developer.apple.com/documentation/foundation/notificationcenter
Simple example
https://medium.com/#JoyceMatos/using-nsnotificationcenter-in-swift-eb70cf0b60fc
Other way reset navigation stack, e.g.
window.rootViewController = someNewNavigationController
Related
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!
I have a two-view login to my app where a user enters two views worth of info. After the user has entered that info I want change which view is the initial view controller, and have the login view be a settings-like page.
How can I change the initial view controller after the user has passed a certain point in the app?
For one of my apps, where I did similar to this, I have my "main" view controller check on viewDidLoad for a setting in the default settings that would indicate if the user had signed in. If they hadn't, I immediately pushed the loginViewController without animation and the user filled in the appropriate forms. When that was dismissed, I reloaded the view in my main view controller.
My client liked the app and it looked nice.
If the user has already "passed a certain point", then what you change to can't be "initial" view controller, can it? The "initial" view controller is the view controller shown on launch.
Do you mean that you want a different view to appear first on subsequent launches? Then write something into the NSUserDefaults that you can check on subsequent launches so that you can start with a different window.rootViewController? Like this (changing all the names) in your app delegate's applicationDidFinishLaunching:
if ([[NSUserDefaults standardDefaults] valueForKey: #"loginDone"])
self.window.rootViewController =
[[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:#"secondVC"];
Or do mean that you just want to navigate away from the login stuff and never return? Then use a presented view controller and just never dismiss it.
I have a UITabBarController, each tabs governs it's own UITableViewController. I have a singleton User object in my app and I also have a logout button in which logs out the user and presents a modalViewController of the login page. When I logout I also have the User object to send a postNotification to the three UITableViewController in order to reset the view and stuff. The question is how do I reset the view so it's fresh like when someone starts with the app again, clearing previous data? What's the best way to do this? Thanks
When each tableview controller receives the notification, it can just empty the data array and reload the table.
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!
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