iOS开发中MVC、MVVM模式详解

来源:互联网 发布:帝国时代3mac版本 编辑:程序博客网 时间:2024/05/01 08:59
iOS中的MVC(Model-View-Controller)将软件系统分为Model、View、Controller三部分



Model: 你的应用本质上是什么(但不是它的展示方式)
Controller:你的Model怎样展示给用户(UI逻辑)
View:用户看到的,被Controller操纵着的

Controller可以直接访问Model,也可以直接控制View。
但Model和View不能互相通信。

View可以通过action-target的方式访问Controller,比如我们在StoryBoard中拖UIButton到代码中所创建的@IBAction,当按钮被点击时,View就会传递该信息给Controller。
有时候Controller需要实时监控View的状态,这时Controller会通过protocol将其自身设为View的delegate,这样当View will change、should change、did change 的时候Controller也会接到相应通知。
View不存储数据,但View可以通过协议获取Controller而不是Model中的数据用来展示。
Controller整理Model中的数据用于给View展示。

Model不能直接与Controller通讯,因为Model是独立于UI存在的。
但当Model发生改变想通知Controller时可使用广播机制,在iOS中有NSNotification和KVO(Key-value observing)可供使用。

NSNotification:

       let center =NSNotificationCenter.defaultCenter()
       
        center.
addObserverForName(UIContentSizeCategoryDidChangeNotification,
            object:
UIApplication.sharedApplication(),
            queue:
NSOperationQueue.mainQueue())
            { notification
in
               
let c = notification.userInfo?[UIContentSizeCategoryNewValueKey]
        }

UIContentSizeCategoryDidChangeNotification以及UIContentSizeCategoryNewValueKey均为系统中定义好的String

KVO:

在ViewDidLoad中:
     webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)

     overridefunc observeValueForKeyPath(keyPath:String?, ofObject object:AnyObject?, change: [NSObject : AnyObject]?, context:UnsafeMutablePointer<Void>) {
             if (keyPath == "estimatedProgress") {
                 progressView.hidden = webView.estimatedProgress == 1
                 progressView.setProgress(Float(webView.estimatedProgress), animated: true)
             }
         }

MVP模式从经典的MVC模式演变而来,将Controller替换成Presenter,依据MVP百度百科中的解释,MVP的优点相比较于MVC是完全分离Model与View,Model与View的信息传递只能通过Controller/Presenter,我查阅资料发现在其他平台上的MVC模式View与Model能否直接通讯有着不同的说法,但在iOS开发中,Apple是这么说的。在MVC下,所有的对象被归类为一个model,一个view,或一个controller。Model持有数据,View显示与用户交互的界面,而View Controller调解Model和View之间的交互,在iOS开发中我按照Model与View无法相互通讯来理解。

MVVM(Model View View-Model



上图展示了MVVM与MVC的差别。

在MVC模式的iOS开发中,Controller承担了太多的代码,包含着我们的视图处理逻辑和业务逻辑。



在MVVM中,我们将视图处理逻辑从C中剥离出来给V,剩下的业务逻辑部分被称做View-Model。
使用MVVM模式的iOS应用的可测试性要好于MVC,因为ViewModel中并不包含对View的更新,相比于MVC,减轻了Controller的负担,使功能划分更加合理。

MVVM模式的正确实践是,我们应该为app delegate的根视图创建一个ViewModel,当我们要生成或展示另一个次级ViewController时,采用当前的ViewModel为其创建一个子ViewModel。

而这些ViewModel的代码并不放在ViewController中,我们的View请求自己的ViewModel来获取所需的数据,ViewController完全归于View。
5 0
原创粉丝点击