开发者

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 p开发者_运维技巧assword 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜