I am creating a Ipad with two view controllers. One view controller is FirstViewController and the other is SecondViewController. In FirstViewController, I fill an array with numbers. Now in my SecondViewCOntroller, I have a table view. I want to put the array that I created in FirstViewController into my SecondViewController table view? How do I do this? Please help me!
3 Answers
You need to reference the NSArray object in the SecondViewController, you could do this by means of a delegate. A delegate is an instance variable that contains the pointer to the delegate, in this case the FirstviewController. Then in FirstViewController you add a property for the NSArray if its an instance variable, and call delegate.someArrayName in the secondviewController
4 Comments
This approach breaks MVC. You can't have data array as an instance variable in your FirstViewController. You'd have to store data in some other class (the M part of MVC). You fill that M part from FirstViewController (the V part) and then access that filled M part from SecondViewController. This way you won't be dependent on how those two controllers relate to each other (parent/child or siblings or whatever other hierarchy you may think of).
The most simple approach I can think of is storing serialized array in a plist file. Storing the file in first and accessing it in the second view controller.
2 Comments
The most straight forward approach will be to create a property on SecondViewController.h like:
@property (nonatomic, retain) NSMutableArray *yourArray;
and in SecondViewController.m, put:
@synthesize yourArray;
At this point you have created a property on SecondViewController. Now, when you are about to open Second View Controller, just create its instance and do something like following:
secondViewController.yourArray = array;
[self.navigationController pushViewController:secondViewController];