Reputation: 438
I am creating an application in which I have to pass data from 16 UITextFields
, store them in an array in form of a class object and display all the objects from that array in other viewController
.
How can you pass data from one viewController
to the another?
Upvotes: 3
Views: 274
Reputation: 36752
Passing to much information from one view controller to the next is a clear tell sign of a bad Model-View-Controller design. Your application logic should be pushed down into the domain layer as much as possible.
I am guessing you are implementing some kind of wizard functionality. Introduce a new FooWizardState
domain object, and pass that along from one view controller to the next. This way you can easily break down your first view controller from 16 text fields to say two controllers with 8 text fields each later if you need to, or the customer demands.
With this design you can also easily change of of the view controllers completely in case one step of the wizard needs to be redone for iPad/iPhone user interface idioms.
Upvotes: 0
Reputation: 10245
Usually communication between unrelated view controllers is a symptom of ugly design. I'd avoid that.
A singleton object might be your best choice. Never ever use the application's delegate or NSUserDefaults
to share data between objects. The same applies for saving data in a plist on disk and reload it from the other controller. That's just extremely bad design.
By the way: whatever is your skill level, get this book, Cocoa Design Patterns. It's not directly related to iPhone development, but it explains Cocoa's design and patterns in a clear way. Understanding it will help you a lot in designing your future applications.
Upvotes: 1
Reputation: 5611
You can do it in an easy way. In the first controller (e.g. FirstViewController
) you can store all the fields in an array:
NSArray *_fields;
Then, you make the array accessible through the @property
construct:
@property (readonly) NSArray *fields;
And in the .m implementation you make them available through the @synthesize
call:
@synthesize fields = _fields;
In the second controller (e.g. SecondViewController
) you should have a pointer to the first one, using the same above constructs:
FirstViewController *_controller;
then
@property(nonatomic, retain) FirstViewController *controller;
and in the .m you will have:
@synthesize controller = _controller;
When you create the second controller, you will have to save the pointer to the first one. E.g.
FirstViewController *first = [[FirstViewController alloc] init];
SecondViewController *second = [[SecondViewController alloc] init];
second.controller = first;
Then you will be able to use second.controller.fields
to read the UITextField instances in the first controller.
Upvotes: 0
Reputation: 1039
There are hundreds of ways to achieve this. It all depends on what you need to do with the data and for how long you need to store it etc.
Upvotes: 0
Reputation: 6402
You can declare array in the second view controller and set the property and synthesize it and then you can pass data to second view controller from the first by
secondviewcontroller *obj = [[secondviewcontroller alloc]init];
obj.array = data;
Upvotes: 0
Reputation: 26390
Suppose you have two view controllers named viewController1
and viewController2
. Have an instance variable textData
of NSArray
in viewcontroller2
. Make sure you add @property
and @synthesize
to the variable. Allocate the array in the init
method of viewController2
and you can pass the array from the first viewController to the second by
viewController2.textData = viewController1.textArray;
Upvotes: 2