1

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?

Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
NKorotkov
  • 3,591
  • 1
  • 24
  • 38
  • Check this post for the copy implementation : http://stackoverflow.com/questions/1459598/how-to-copy-an-object-in-objective-c – Boris Charpentier Nov 19 '14 at 13:49
  • It's a bit strange to me that a userProfile should not be changed everywhere in your app, why do you want that ? – Boris Charpentier Nov 19 '14 at 13:51
  • @Boris Charpentier as I said it's only nedeed when back button (or gesture) is pressed and changes have to be discarded. – NKorotkov Nov 19 '14 at 14:02
  • Ok so I would definitly put my userProfile in a singleton to access it anywhere rather than passing it around and have copy everywhere. Then you should have a smart way of discarding changes, I never used it but there is a NSUndoManager class in iOS https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUndoManager_Class/index.html#//apple_ref/occ/cl/NSUndoManager that could help you – Boris Charpentier Nov 19 '14 at 14:06
  • @Boris Charpentier I considered a singleton myself, but everyone recommends against using a singleton just to pass data between controllers. Seems like singletons should not be used unless it's really necessary. – NKorotkov Nov 19 '14 at 14:17
  • Well you can still pass it around and use an undomanagement system if you like. Like everything, abuse is bad, and abuse of singleton certainly is. But if you want an unique object accessible in every controller of your app... well you'r close of a singleton use case – Boris Charpentier Nov 19 '14 at 14:26

0 Answers0