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?
You are creating an instance of
FirstViewController, setting the value ofarrayand then releasing that instance which will deallocate the object. So the entire effort is wasted. Since this is in theSecondViewController, I am assuming theFirstViewControllerexists by this time so you shouldn’t be setting thearrayof a new instance ofFirstViewControllerbut 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
SecondViewControllerinstance inFirstViewController,Now the array is shared across the view controllers. Do not initialize the
arraySecondproperty elsewhere so that both of them keep pointing to the same object and the changes your make toarraySecondare visible to theFirstViewControllerinstance. After coming back to theFirstViewControllerinstance, access the values you’ve added usingarrayproperty.Alternatives to object sharing are delegate mechanism and notifications. You can look into them too. For now, this should work.