0

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 3

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

So what you're saying is that in my FirstViewController. I have a variable like this: FirstViewController *firstViewVariable; then add the property. Then call it in SecondViewController? I'm a little confused, is this right?
@serge2487 yeah my answer is not really easy if you do this for the first time.. eh you know what a delegate is and how to use that?
i am using delegate and datasource methods for my tableview. I know how to fill my table but only if its all done in my SecondViewcontroller. I think so lol.
Well you add a variable to the SecondViewController that contains the delegate.. "id delegate;" "@property (nonatomic, assign) id delegate;" in the header "@synthesize delegate;" in the source file and then when you create SecondViewController you do this: secondViewControllerVar.delegate = FirstViewController; and then you can go from there, as you can access the FirstViewController in the secondViewController
1

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

i see. how would store it in a plist. does that mean the array can't change? Because I would be changing its content pretty often.
You can change the array. You'd just overwrite plist contents. There are convenience methods for this. Just check the documentation.
1

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.