IOS MVC - transfer data back and forth to view controllers and models

I am new to iOS programming and have written a simple reminder style application, now I rewrite it to correctly implement the MVC model, since previously all my codes were inside the view controllers.

I have my own Event class with the name of the property, time, repetition, etc., and then the following structure:

Model class

Retrieves, processes, and saves data in NSUserDefaults

Rootootcontroller

Creates an instance of the Model object and asks the model to return all Events objects from NSUserDefaults , and then displays them in a UITableView

EditEventViewController

[editEventVC initWithEvent:theEvent];

Skips the specific event object that was selected in the table cell by the init method and displays all editable properties

EditEventPropertyViewController

[editEventPropertyVC initWithValue:propertyValue];

Skips the value of the property for editing (for example, the name of the event) using the init method and returns the updated value of the user through the delegation method

Is this the right way to implement this application?

What is the best way to save an updated Event object to NSUserDefaults via the model after completion with EditEventViewController? Through the delegate? I am currently reloading the uitableview data on viewWillAppear in the root controller, so it will need to save the updated data before loading it again.

thanks

+1
source share
1 answer

You can save the Event collection to NSUserDefaults . From its user class, you need to implement the NSCoding protocol for serialization to NSUserDefaults .

 //Event.h @interface Event : NSObject<NSCoding> @property (nonatomic, copy) NSString *name; @property (nonatomic, strong) NSDate *time; @property (nonatomic) NSInteger repeat; - (void)save; + (NSArray *)allEvents; //Event.m #define kSavedEvents @"SavedEvents" #pragma mark - Encoding - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.name forKey:@"EventName"]; [encoder encodeObject:self.time forKey:@"EventTime"]; [encoder encodeObject:@(self.repeat) forKey:@"EventRepeat"]; } #pragma mark - Decoding - (id)initWithCoder:(NSCoder *)decoder { self = [super init]; if (self) { _name = [decoder decodeObjectForKey:@"EventName"]; _time = [decoder decodeObjectForKey:@"EventTime"]; _repeat = [[decoder decodeObjectForKey:@"EventRepeat"]integerValue]; } return self; } 

You need to archive data during storage in NSUserDefaults and unarchive when retrieving

  (void)save { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *savedData = [defaults objectForKey:kSavedEvents]; NSMutableArray *savedEvents = [@[] mutableCopy]; //Some events are already there in there if (savedData) { savedEvents = [[NSKeyedUnarchiver unarchiveObjectWithData:savedData]mutableCopy]; } [savedEvents addObject:self]; //Archiving the savedEvents to data NSData *newEventData = [NSKeyedArchiver archivedDataWithRootObject:savedEvents]; [defaults setObject:newEventData forKey:kSavedEvents]; [defaults synchronize]; } + (NSArray *)allEvents { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *data = [defaults objectForKey:kSavedEvents]; if (data) { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; } return nil; } 

Hope this helps you get started.

+1
source

All Articles