When programming for iphone how do you use data for multiple views?
I am a objective-c newbie and I was wondering how do you use data obtained from one view in another? For example I ask a user for a text input, and this text input then changes a label when a button is hit. I want thi开发者_高级运维s information for another label in a different view. how would I get it? Thanks
There are many ways to share data across views (or classes) in objective-c. One common method is to create a Singleton object that functions as a global object for your application. Here's a great tutorial about using Singletons in objective-c for global data.
When the text input finishes, it tells the view controller. A view controller can control a number of views, or even control other controllers. When it gets the notification, it can tell the other views about it.
You do this by having the text field trigger an action in your controller.
You can do this directly, or you could set up "bindings".
If you are doing something simple like passing a value from one view controller to another, you can just store a reference to the first view controller in your second view controller, and this would then give you access to the first view controller's ivars. Here is a blog post I did on this very subject:
http://www.dosomethinghere.com/2009/10/04/passing-values-and-messages-between-views-on-iphone/
精彩评论