標籤:
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:
1 let center = NSNotificationCenter.defaultCenter()2 3 center.addObserverForName(UIContentSizeCategoryDidChangeNotification,4 object: UIApplication.sharedApplication(),5 queue: NSOperationQueue.mainQueue())6 { notification in7 let c = notification.userInfo?[UIContentSizeCategoryNewValueKey]8 }
KVO:
1 webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)2 3 override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {4 if (keyPath == "estimatedProgress") {5 progressView.hidden = webView.estimatedProgress == 16 progressView.setProgress(Float(webView.estimatedProgress), animated: true)7 }8 }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的負擔,使功能劃分更加合理。
IOS的MVC和MVVM模式簡明介紹