objective-c - 应该如何使用 Objective-C 在 iOS 中实现模型?

标签 objective-c ios cocoa-touch model-view-controller model

EDIT: Although the answers are teaching me things, I still have no idea how I might "implement" a model. Can anyone give me an example as to how I would create a simple model class that has a few basic functions that make calls to NSUserDefaults or JSON web calls and how I would access this from ANY of my view controllers?

我刚刚开始 iOS 开发,已经到了我的应用程序需要模型与 Controller 之间的整体数据流交互的地步,但我不确定应该如何正确实现它们。

我知道 View 在 Storyboard中,而 Controller 是与这些 View 关联的类。

为应用程序实现中央模型的正确方法是什么?我是否创建一个类(即“MyModel.h/.m”)然后从我所有的 View Controller 导入它?

我还看到有人使用 UINavigationController->RootViewController 作为他们的模型,这可行吗?

我用谷歌搜索了这个问题并搜索了几个小时的堆栈溢出,但现在求助于一个新问题。

EDIT: Although the answers are teaching me things, I still have no idea how I might "implement" a model. Can anyone give me an example as to how I would create a simple model class that has a few basic functions that make calls to NSUserDefaults or JSON web calls and how I would access this from ANY of my view controllers?

最佳答案

在 iOS 中,模型(MyModel 类)通常是 NSObject 的子类,或者在 Core Data(帮助将数据保存到设备本地数据库的 iOS 框架)NSManagedObject 的情况下。与任何模型对象一样,它包含实例变量和getter/setter 方法。大多数面向对象的语言都有提供封装的机制,在 iOS 中,property 提供封装,关键字 synthesize 自动生成 getter 和 setter 方法.

View 是 *UIView* 的子类,它提供了处理触摸事件和绘图的能力。 UIKit 框架 包含用于绘制典型界面元素的类,例如表格(列表)、按钮、文本字段、 slider 等。

Controller 通常是管理 View 的 **UIViewController** 的子类, 它还负责响应委托(delegate)消息和目标操作消息。您可以拥有一个 UITableViewController,它是一个子类管理 UITableView

UIViewController

TabBar 和 Navigation View Controllers 管理一组 View Controller ,但 Navigation VC 将 VC 作为“stack”数据结构进行管理,是的,它是一个可行的用法

请看Design Patterns 在 ios 苹果库资源中供进一步引用和 here是苹果示例代码,了解如何使用模型- View - Controller 设计模式创建网络应用程序

this教程教您如何开始使用 JSON ,尝试将 FB 集成到您的应用程序中以了解 JSON 的乐趣和简单

例如,开始在您的应用中编写 NSUserDefault

// create a standardUserDefaults variable
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];

// saving an NSString
[standardUserDefaults setObject:@"mystring" forKey:@"string"];

here是我开始学习的一个很好的教程..

快乐编码:)

关于objective-c - 应该如何使用 Objective-C 在 iOS 中实现模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12633948/

相关文章:

ios - iOS:按最近的年份(忽略年份)对NSDate数组进行排序

ios - 如何检查 SVG 文件验证?

iphone - 如何在 iPhone 上将图像转换为视频?

iphone - iOS应用启动或切换回前台的速度非常慢

ios - 检查字符串时间范围是否为现在

iOS - UITableViewCell 的隐藏 UIImageView 是否应该在可见之前没有分配的图像?

objective-c - iOS - 从数据库创建动态用户界面

ios - 如何在 Xcode8 上使用 Swift 2.2?

ios - 我们如何在 iPhone map View 中将图像添加为折线?

objective-c - 滚动 UIScrollView 以显示几个文本字段中的最低部分