ViewController data sharing - iphone

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

Related

How to refresh all view controllers in swift

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

Segue to Tab Bar Controller with performSegueWithIdentifier

I'm learning iOS and I'm working on what amounts to a Proof of Concept app in XCode 4.2. I essentially want to allow a user to enter a user/pass combo and if the user is in an array of objects, pass them on to the "Content" view, but if not, pass them on to the "Register" view.
In either case, since the app itself will be a tabbed app (though the login and register views are not tabbed), I need to be able to segue from a typical ViewController to a TabViewController.
- (void)logIn:(id)sender{
[self performSegueWithIdentifier:#"Content" sender:sender];
}
Currently, I have the app segueing to the proper ViewController, which is a TabBarController embedded in a NavViewController...but once it gets there...there are no tabs!!
What am I doing wrong?
Now you aren't loading TabBarController that's why you don't have any tabs.
What I suggest you:
In Log In ViewController you can also add "register" button, and place a segue from it to Register View Controller.
Add IBAction from log in button, which will check for log in and pass, and if it's OK load Content ViewController or TabBar Controller with Content ViewController.
I strongly recommend you to study iOS5 storyboard tutorials to get to know how the view controllers interacts.

how to insert UISplitView inside one my View Based application

I am working on an iPad application and need some help from you guys.
Actually i want to use UISplitView inside one my View Based application.
The flow of my app would be like following:
In main view:
When i Enter username and password and click Login, On Successfull login it should open the second screen using present model view controller.
Now on Second Screen there is a Button to goto Mails. When i click on it It should open up the 3rd screen. again pushed using presentModalViewController, which should have a UISplitViewController to show the emails list and when clicking on any email show the detail of that email.
Now please can any one guide me how can i use uisplitView controller inside the Viewbased application templet.
at least post any use full links/source code files.
Thanks in advance
The SplitViewController has to be the RootViewController. From Apple Docs:
"A split view controller must always be the root of any interface you create. In other words, you must always install the view from aUISplitViewController object as the root view of your application’s window. The panes of your split-view interface may then contain navigation controllers, tab bar controllers, or any other type of view controller you need to implement your interface."
So you cannot do what you want without writing your own container views (in iOS5) instead of using Apple' SplitViewController.

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!

How do I get a login screen on an iPhone app (using tabBarController)?

I am developing a web app (using rails) that I will be launching within a week. I've "jumped the gun" and started working on the iPhone app for the site (I'm new to iPhone development). Using the peepcode iPhone screencasts and the expense/budget demo app from clarkware I've been able to get things going (using ObjectiveResource for iPhone-to-Rails communication). So I'm able to get a tabBarController loading a table with test data populated from my staging server online. Great.
My problem lies in that I need login functionality. The budgets demo app has this nailed but it uses a navigationController. I would like to use a tabBarController (which I'm using currently) to handle the basic functionality of the app.
Here's how I see the app login functionality working when completed:
When a user first runs the iPhone app, the iPhone app will present a login screen (username and password). If a correct username and password is entered the session/user info is saved (preferably to the general/settings app section of the iPhone). The user won't be presented with the login screen again unless the session expires, the user edits the username/password in the general/settings section of the iPhone, or the user deletes the application and reinstalls.
The closest thing to what I have in mind for this process is the Gowalla app.
So I suppose my question is: What is the best way to get a login screen to appear when using a tabBarController? Once I can do this and get authentication taken care of the rest should fall into place.
Please let me know if there's anything I need to clarify - THANK YOU!
-Tony
Do you want to have the tab bar to display at the bottom? Then just put the Login View Controller as the selected tab by setting the selectedViewController or selectedIndex properties of UITabBarController.
Do you want to have the tab bar display at the bottom but not be actionable until they've logged in? Then set a UITabBarDelegate and override the tabBarController:shouldSelectViewController: method to disallow until login has happened.
Do you not want the tab bar to display until login has been entered (personally I would think this is most desireable)? Then either don't show the TabBarController until you're ready, show the Login View Controller instead... or, alternatively, show the Login Controller as a Modal View Controller over the TabBarController and don't allow it to be dismissed until login was successful.
I am new to iPhone programming, but have done C++ coding moons ago. So far I have created everything programmatically rather than use the nib approach.
For what you what to achieve, I would create a main view controller from the app delegate. Then from this main view controller I would create the login view (UIView) that initially displays and seeks the users login credentials. Once validated, your main view contoller can the create and display (or just display if you created it initially) a tab bar controller with the appropriate view controllers for each tab.
So appDelegate -> Main View Controller -> Logon View
-> Tab View Controller + View Controllers
Hope this helps?!
Matthew.