views:

89

answers:

1

Hi there!

Maybe a FAQ at this website.

I have a TableViewController that holds a form. In that form I have two fields (each in it's own cell): one to select who paid (single selection), and another to select people expense is paid for (multiple selection).

Both fields open a new TableViewController included in an UINavigationController.

Single select field (Paid By) holds an object Membership Multiple select field (Paid For) holds an object NSMutableArray

Both vars are being sent to the new controller identically the same way:

mySingleSelectController.crSelectedMember = self.crPaidByMember;
myMultipleSelectController.crSelectedMembers = self.crSelectedMembers;

From Paid for controller I use didSelectAtIndexPath method to set a mutable array of Memberships for whom is paid:

 if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) {
  [self.crSelectedMembers removeObject:[self.crGroupMembers objectAtIndex:indexPath.row]];
  //...
 }
 else {
  [self.crSelectedMembers addObject:[self.crGroupMembers objectAtIndex:indexPath.row]];
  //...
 }

So far everything goes well. An mutable array (crSelectedMembers) is perfectly set from child view. But... I have trouble setting Membership object.

From Paid By controller I use didSelectAtIndexPath to set Membership:

[self setCrSelectedMember:[crGroupMembers objectAtIndex:indexPath.row]];

By NSlogging crSelectedMember I get the right selected member in self, but in parent view, to which ivar is pointed, nothing is changed.

Am I doing something wrong? Cause I CAN call the method of crSelectedMembers, but I can't change the value of crSelectedMember.

A: 

If I understand your question, the most likely cause is an improper property declaration.

If you want to pass values from one object to another using each objects properties, then you need to make sure to use assign to ensure the properties in one object are pointing at the same instances as the property in the other object.

So in your topViewController you have a property:

@property (nonatomic,retain) NSString crSelectedMember;

Then in your child view controllers you have:

@property (nonatomic,assign) NSString crSelectedMember;

This forces the value into the exact object in the parent controller.

However, this is a very fragile way to pass data between viewControllers. As your app becomes more complicated, it will be impossible to track all the passed data. (Worse, if you run into memory limitations, the parent view controller may unload and not even exist when you try to pass data to it.)

Instead, you should have a single custom object devoted to holding your data. Each view controller should query that object for the data it needs and should write any changes back to that object. The view controllers never communicate directly. This technique allows you to control the data in one specific location instead of spreading it out all over your code and it scales well. You can add an arbitrary number of view controllers to you app without having to worry about tying them all together.

See this post for details: http://stackoverflow.com/questions/2363777/iphone-how-to-pass-data-between-several-viewcontrollers-in-a-tabbar-app/2364524#2364524

TechZen
Thanks! I think Singleton approach mentioned in linked post will do the trick.
Ivan