iOS passing object between View Controllers with strong reference

415 views Asked by At

I have a series of VC dedicated to profile editing in a navigation stack. As you go deeper a custom object userProfile is passed in -prepareForSegue. My problem is in this configuration all userProfiles seems to be pointing to a single object, and if back button is pressed after changes have been made to current Profile, Profile in parent controller is changed too.

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

     if ([segue.identifier isEqualToString:@"ProfileToEdit"]) {

         EditProfileTableViewController *editProfileVC = [segue destinationViewController];
         editProfileVC.userProfile = self.userProfile;
         editProfileVC.delegate = self;

     }

Each VC has a property declared in it's .h file like this:

@property (strong, nonatomic) UserProfile *userProfile;

Where userProfile is a very simple class

@interface UserProfile : NSObject

@property (strong, nonatomic) NSMutableArray *cars;
@property (strong, nonatomic) NSString *phoneNumber;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *currentPassword;

@end

As I see it the solution lies in making each controller to hold it's own copy of the object. I'm not sure how to implement it correctly. Will this solve my problem?

    @property (copy, nonatomic) UserProfile *userProfile;

If yes, how should I implement -copy method in my custom object, since it doesn't have one?

0

There are 0 answers