用于操作 NSMutableArray 的 IOS MVC 模式和数据 Controller

标签 ios xcode model-view-controller nsmutablearray

我有:
一个叫做 Care 的类
一个名为 masterCareList
的 NSMutableArray masterView, detailView
careDataController 用于将 Cares 添加到列表并计算对象数量。

我想在 MasterView 中计算我的对象,我想在 detailView 中添加一个对象。在这两种情况下都使用 careDataController(不倒带转场或在 View 之间传递数据)并使用相同的 masterCareList。

我如何以最佳 (MVC) 方式使用相同的 dataController 并因此使用相同的 NSMutable 数组?

最佳答案

您可以在应用程序的初始设置时创建您的 careDataController,并通过 MasterView 的属性访问 careDataController。这将使您能够独立于 View Controller 封装对数据的计数和其他操作。

一个非常高级的示例,您可以在其中创建 MasterView...AppDelegate 可能...

//Appdelegate.m file
MasterView *mv = [[MasterView alloc] init];
DataController *careDataController = [[DataController alloc] init];
mc.model = careDataController;  //model is a property within MasterView that you create

[self presentViewController:mv animated:YES completion:nil];

现在,您基本上可以在创建 DetailView 并将其模型属性设置为您在上面设置的 MasterViews 模型属性后,在 MasterView 中呈现它。

//some procedure in MasterView.m
DetailView *dv = [[DetailView alloc] init];
dv.model = self.model; //self.model you set above

[self presentViewController:dv animated:YES completion:nil];

现在在 DetailView 中,您可以将护理对象添加到您最初在应用启动时创建的原始 careDataController...

//somewhere in DetailView.m
Care *careObject = [[Care alloc] init];
[self.model addObject:careObject];

当您在完成添加对象后关闭 DetailView 时,MasterView 中的 careDataController(模型属性)现在将具有您在 DetailView 中添加的新护理对象。

编辑: 我看到了你的新评论,在某种程度上,它本质上是我刚刚在上面写的,我认为它没有错。您可以做的另一件事是不要像上面那样传递它,而是让 MasterView 成为 DetailView 的委托(delegate)。当您添加一个 Care 对象时,将该对象状态保存在 DetailView 中,并使用您在 DetailView 中定义的协议(protocol)将其传回。您可能在 MasterView 实现的协议(protocol)中有如下所示的方法...

-(void)detailView:(DetailView *)dv didInsert:(Care*)careObject
{
    [self.model addObject:careObject];
}

关于用于操作 NSMutableArray 的 IOS MVC 模式和数据 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16068690/

相关文章:

ios - NSData 对象因未知原因为空(保存图像)

iOS Swift 标签框架高度未正确更新

swift - Storyboard 未链接到 ViewController.swift Xcode 中的 @IBAction/Outlet

来自设备上部署的应用程序的 iOS os.log

jquery - 验证最多 x 位大于零的小数值

php - 如何在 MVC 框架中有效地实现模块,并在单个模块中处理到多个 Controller 的路由?

c# - 上传mvc文件时在浏览器中刷新c#

ios - 当键盘关闭时,如何使搜索栏中的取消按钮保持启用状态?

ios - SwiftUI .popover 框架大小不正确

ios - 为什么我收到这个 clang 错误 : _fwrite$UNIX2003 on simulator with iOS 8