ios - 实现 MVC 模式

标签 ios swift model-view-controller

我在使用 Swift 在 iOS 中实现 MVC 模式时遇到了问题。根据 Apple 文档,有一个 MVC 模式:

MVC pattern

我对此很好,但正如您所见,当模型自身发生变化时(例如,来自套接字的传入消息)它应该如何通知 Controller ?

例如,我有一个聊天应用程序,其中包含一个表示消息列表的模型。当模型收到一条新消息时,它如何通知 Controller 呢?有没有传统的方法可以做到这一点?

谢谢

最佳答案

您可以通过两种方式实现此模型- Controller 通信。

  1. 委托(delegate)模式
  2. 通知(https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/index.html)

有关详细说明,我建议您观看 CS 193p MVC 讲座。 (https://www.youtube.com/watch?v=Cb8KtEI3ZaY)

关于ios - 实现 MVC 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555345/

相关文章:

ios - 下载和使用缓存图像时按顺序将图像加载到数组中

ios - 以编程方式使用自动布局

ios - 尽管桥接头工作正常,但无法在 Swift 中实例化 Obj C 类

找不到 Spring 框架错误页面

model-view-controller - 使用 Grails 时,我应该关心 Open Session in View 吗?

ios objective-c : How to get NSURLSession to return Content-Length(http header) from server

ios - 在应用程序启动之间保留数据

ios - 在第二次调用期间将数据填充到方法中的字典时出错

swift - 自动续订订阅免费试用无效

ruby-on-rails - rails : calling a model class in a library