0

So i have two view controller in FirstViewController and SecondViewController

in FirstViewController

 NSMutableArray *array;

create a property for it and synthesised it

in SecondViewController

NSMutableArray *arraySecond;

create a property and synthesized it then I try to do something like this (after arraySecond is set to something like a b c d )

FirstViewController *FV = [[FirstViewController alloc]init];
FV.array = arraySecond;
[FV release];

I try to do that but when I try to print out the array from firstviewcontroller it is being set to (null) why is it doing that and what can i do to fix it?

2
  • How exactly do you do the synthesis? Commented Jun 25, 2011 at 8:41
  • @synthesis array; @synthesis arraySecond; Commented Jun 25, 2011 at 8:44

2 Answers 2

1
FirstViewController *FV = [[FirstViewController alloc]init];

secondViewController *SV = [[secondViewController alloc]init];

//here create secondArray

[FV SetArray:SV.arraySecond];

If you synthesized the arraySecond then [self.arraySecond release];

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

3 Comments

AFAIK Obj-C is case-sensitive, so you should mind you don't copy SetArray but instead use setArray (note the lowercase s).
I went [FV.array setArray:SV.arraySecond];
if u synthasize the array then u can use with dot syntax.otherwise u have to use like this.
0

You are creating an instance of FirstViewController, setting the value of array and then releasing that instance which will deallocate the object. So the entire effort is wasted. Since this is in the SecondViewController, I am assuming the FirstViewController exists by this time so you shouldn't be setting the array of a new instance of FirstViewController but try to pass it to the existing instance. Since you already have a property declared to share across the view controllers, we will make use of it.

Do this when instantiating the SecondViewController instance in FirstViewController,

SecondViewController * viewController = [[SecondViewController alloc] init];

self.array = [NSMutableArray array];
viewController.arraySecond = self.array;

[self.navigationController pushViewController:viewController animated:YES];
[viewController release];

Now the array is shared across the view controllers. Do not initialize the arraySecond property elsewhere so that both of them keep pointing to the same object and the changes your make to arraySecond are visible to the FirstViewController instance. After coming back to the FirstViewController instance, access the values you've added using array property.

Alternatives to object sharing are delegate mechanism and notifications. You can look into them too. For now, this should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.