Roselle Tanner
Roselle Tanner

Reputation: 367

passing data to viewcontrollers

I have a mutable array of values used in a tableviewcontroller, a mapviewcontroller, and the callingviewcontroller. I have been updating them all, ie.

[dataArray addObject:object atIndex:0];
[tvc. dataArray addObject:object atIndex:0];
[mvc. dataArray addObject:object atIndex:0];

Is there a way of declaring dataArray in the table and map viewcontrollers that would make them pointers to the dataArray in the callingViewController? So I would just have to update one?

***Okay guys, I made a really stupid mistake here. At some point I changed the initialization and passed nil as the dataArray, and for some reason I had an "if (!dataArray) create new" clause to hide it from myself.

Kaan is correct. [dataArray addObject:object atIndex:0]; is all that is needed.

Upvotes: 0

Views: 102

Answers (3)

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14841

After your comment:

How about you declare a property called dataArray in both your tableView and mapView and also callingView. Then, in your callingViewController, you initialize a mutable array called lets say myFirstArray and do:

NSMutableArray *myFirstArray = [NSMutableArray array];
self.dataArray = myFirstArray;
myTableView.dataArray = myFirstArray;
myMapView.dataArray = myFirstArray;

This way they will both be pointing to the same object and change you do in one will reflect in the other. Give it a try!

Upvotes: 0

user523234
user523234

Reputation: 14834

if you create the array in the AppDelegate class then you can access from any other classes within your app.

Upvotes: 0

Dancreek
Dancreek

Reputation: 9544

This is a little more involved but I think would be the best solution. You should have some way for all of these different ViewControllers to reference a single object that is managing your data. This could be a delegate, or it could be a singleton that owns the main dataArray.

Search here or on google for both of those terms and you should be able to get started with either route.

Upvotes: 1

Related Questions