iOS MVC - 来回传递数据以查看控制器和模型 [英] iOS MVC - Passing data back and forth to view controllers and model

查看:130
本文介绍了iOS MVC - 来回传递数据以查看控制器和模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS编程新手并编写了一个简单的提醒样式应用程序,我现在正在重写它以正确实现MVC模型,因为之前我的所有代码都在View Controllers中。

I'm new to iOS programming and having written a simple reminders style app, I'm now rewriting it to implement the MVC model correctly as previously all my code was inside View Controllers.

我有一个名为Event的自定义类,包含属性名称,时间,重复等,然后是以下结构:

I have a custom class called Event with properties name, time, repeat etc and then the following structure:

模型类

检索,处理和保存数据 NSUserDefaults

Retrieves, processes and and saves data to and from NSUserDefaults

RootViewController

创建Model对象的实例,并要求模型从<$ c $返回所有Events对象c> NSUserDefaults 然后将它们显示在 UITableView

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

EditEventViewController

[editEventVC initWithEvent:theEvent];

通过init方法传递在表格单元格中选择的特定事件对象,并显示可用于编辑的所有属性

Passes the specific event object that was selected in the table cell via init method and displays all the properties available for editing

EditEventPropertyViewController

[editEventPropertyVC initWithValue:propertyValue];

传递要编辑的属性值(例如事件名称)通过init方法并通过委托方法返回用户更新的值

Passes value of property to edit (e.g. event name) via init method and returns the user updated value via a delegate method

这是实现此应用程序的正确方法吗?

Is this the correct way to implement this app?

使用EditEventViewController完成后,通过模型在 NSUserDefaults 中保存更新的Event对象的最佳方法是什么?通过代表?目前我正在rootViewController中的viewWillAppear上重新加载uitableview数据,因此它必须在再次检索之前保存更新的数据。

What's the best way to save the updated Event object in NSUserDefaults via the Model after finishing with the EditEventViewController? Via a delegate? Currently I am reloading the uitableview data on viewWillAppear in the rootViewController so it will have to save the updated data before retrieving it again.

谢谢

推荐答案

您可以在 NSUserDefaults 事件的集合C>。由于它是一个自定义类,您需要实现 NSCoding 协议,以便序列化为 NSUserDefaults

You can store the collection of Event in NSUserDefaults. Since its a custom class, you need to implement 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;
}

您需要在存储到NSUserDefaults时存档数据并在获取时取消存档

You need to archive the data while storing to NSUserDefaults and unarchive when fetching

 (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;
}

希望这能让你入门。

这篇关于iOS MVC - 来回传递数据以查看控制器和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆