3

I have a RootViewController and a DetailViewController where I am trying to pass a NSMutableArray from the rootView to the detailView. I have created an object of the RootViewController inside the DetailViewController and accessing it like the following

RootViewController *root = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

detailViewArray = [root.rootViewArray copy];

Note: Both the arrays are declared in the .h files; synthesized and then allocated and initialized array = [[NSMutableArray alloc] init];

Problem: I am not too sure why it still doesn't work. I have tried a lot of the solutions on the internet but it didn't quite work for me. The property for the root array is nonatomic, retain Is it something wrong with that? Do I need to change it to something or the method I am following is just not right.. Please if someone could help!

2 Answers 2

3

If you alloc/init the RootViewController inside the DetailViewController, you are creating another instance of the RootViewController. You are not getting the same instance (with data) of the rootViewController.

That said, even passing a reference of a viewController to another viewController to then poke at it's data is sort of bad. It creates tight coupling between the views.

Instead, if you need to get data, consider using a delegate to communicate between the views.

What exactly does delegate do in xcode ios project?

Tutorial:

http://www.theappcodeblog.com/2011/04/15/passing-data-between-views-tutorial-using-a-protocol-delegate-in-your-iphone-app/

Another option is to create a shared model (read up on model view controller patterns). It's typical in that pattern to create a model and share data by getting a singleton instance of your model:

MyModel *model = [MyModel sharedInstance];

Then, each view can set and read data from that same (singleton) instance of the model.

Which to choose? The model is better if many views share the same data. A delegate is appropriate for a couple views to communicate with each via callbacks.

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

2 Comments

Even there is an another way to pass array. MyViewController mvc = [[MYViewController alloc] initWithNibName:@"MYViewController" bundle:[NSBundle mainBundler]]; [mvc getDataArray:<Pass my array>];
@mrunal - that's exactly why apple recommends delegates. Having one view directly set data on another creates tight coupling of your views.
0

Even there is an another way to pass array.

MyViewController mvc = [[MYViewController alloc] initWithNibName:@"MYViewController" bundle:[NSBundle mainBundler]];

[mvc getDataArray:<pass your array>];

[self.navigationController pushViewController:mvc animated:YES];

Here it will set a dataArray property first then it will push the view controller to MyViewController class.

This will be useful when you are very much concerning about memory. Because if you are using static (sharedInstance) then its scope persistance within the entire application scope.

4 Comments

if you want to pass it directly you should use delegates. setting it directly creates tight coupling of your views.
@bryanmac: Oh I dont have that much idea about delegates. Can you please give code for an example ?
in my answer above there's a couple links.
@Mrunal: your solution does not work. I have tried this, you can set only int, float values, but can not set the Array or string values. At least I could not do this.

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.