mvc2 mvc_迅捷的MVC
mvc2 mvc
Prerequisites:
先決條件:
- Some understanding of OO terminology and practices 對OO術語和實踐的一些理解
Terminology
術語
Model: Where data, and logic that manipulates the data is stored. Perhaps model objects, or networking code is stored here.Think of this as the what of the App.
模型:數據和操作數據的邏輯存儲在何處。 也許模型對象,或網絡代碼存儲的這個here.Think作為在App的東西 。
View: Present information to the user. Views are, well, UIViews and their subclasses. Think of it as the UI components that have to be controlled by the controller.
查看:向用戶顯示信息。 視圖是UIView及其子類。 可以將其視為必須由控制器控制的UI組件。
Controller: Sits between the view and the model, tying them together (usually using the delegate pattern). The controller is not tightly bound to a concrete view, and communicates via a protocol to an abstraction. An example of this is the way that a UITableView communicates with its data source through the UITableViewDataSource protocol. Think of it as the how of the App. The primary job of the controller is to format the data from the model for the view to display.
控制器:位于視圖和模型之間,將它們綁在一起(通常使用委托模式)。 控制器未緊密綁定到具體視圖,而是通過協議與抽象進行通信。 一個示例就是UITableView通過UITableViewDataSource協議與其數據源進行通信的方式。 你可以把它作為應用程序的方式 。 控制器的主要工作是格式化模型中的數據以供視圖顯示。
Mediator:
調解員:
The Communication
溝通交流
In MVC the controller can talk to the model (publicly available functionality).
在MVC中,控制器可以與模型對話(公開可用的功能)。
Perfectly acceptable communication from the controller to the model 從控制器到模型的完全可接受的通信The controller needs to be able to talk to the view through outlets. Equally a view can communicate back to a controller (for example through target-action, or a delegate). This means that the views can be reused, as they are loosely coupled with any particular controller. Views do not own the data they are displaying — this is the use of datasource for UITableView.
控制器需要能夠通過插座與視圖對話。 同樣,視圖可以與控制器通訊(例如,通過目標動作或委托)。 這意味著視圖可以重用,因為它們與任何特定的控制器松散地耦合在一起。 視圖不擁有它們所顯示的數據,這是對UITableView的數據源的使用。
Communication from the controller to the view is typical 從控制器到視圖的通信是典型的However, we should STOP any potential communication from the model to the view. This really makes sense because views are often particular instances of (for example) a UISlider and a UISlider has no idea of the logic from a calculator (or whatever is being built).
但是,我們應該停止從模型到視圖的任何潛在通信。 這確實很有意義,因為視圖通常是(例如)UISlider的特定實例,而UISlider不了解計算器(或正在構建的任何東西)的邏輯。
Models should not communicate with the view 模型不應與視圖進行通信The MVC model tends to go with one screen.
MVC模型傾向于只使用一個屏幕。
Communicate with multiple MVCs — treat each view as a single MVC.
與多個MVC通信-將每個視圖視為一個MVC。
Example
例
This is going to step through 4 examples to retrieve data from a simple API. Each will use a different method to do so. These are tied in a Tab Bar Controller.
本文將逐步介紹4個示例,以從簡單的API檢索數據。 每個人都將使用不同的方法來這樣做。 這些都綁在標簽欄控制器中。
Each view controller has a button, and once pressed
每個視圖控制器都有一個按鈕,一旦按下
Method 1 — API call within the view controller
方法1-視圖控制器中的API調用
This method is commonly used in tutorials and through simple examples of MVC.
此方法通常在教程中以及通過簡單的MVC示例使用。
Unfortunately the view controller needs to know about the baseUrl that is being accessed.
不幸的是,視圖控制器需要知道正在訪問的baseUrl。
We can do better!
我們可以做得更好!
Learning from the separation of concerns, we want to have the HTTP moved outside the view controller
從關注點分離中學習,我們希望將HTTP移動到視圖控制器之外
So we set up a TightlyCoupledHTTPManager class which will be called from the view controller.
因此,我們設置了一個TightlyCoupledHTTPManager類,該類將從視圖控制器中調用。
Unfortunately there is no way to get the data out to the view controller, for example in the implementation below:
不幸的是,無法將數據發送到視圖控制器,例如在以下實現中:
httpManager.data.count will always be 0. This is because it takes time to make the API call, and as we move through the code above there isn’t time to make sure the data is populated. Any solution (using a timer to wait) is flaky and does not guarantee that the data is populated at any point in time.
httpManager.data.count始終為0。這是因為進行API調用會花費一些時間,并且隨著我們遍歷上面的代碼,沒有時間來確保填充數據。 任何解決方案(使用計時器等待)都是不穩定的,并且不能保證在任何時間點都會填充數據。
Method 2 — Delegate pattern
方法2-委托模式
This pattern means that you can receive the data in any class that conforms to the relevant protocol, that is in this case:
此模式意味著您可以在符合相關協議的任何類中接收數據,在這種情況下:
The new HTTPManager (DelegationHTTPManager) calls the delegate when it has completed:
新的HTTPManager(DelegationHTTPManager)在完成后會調用委托:
Meaning that the function didDownloadBreaches is called by the delegate.
意味著該函數didDownloadBreaches由委托調用。
However for this to happen we have to set the view controller as the delegate:
但是,要做到這一點,我們必須將視圖控制器設置為委托:
Method 3 — Closures
方法3 —閉包
This ClosureHTTP Manager uses the new Result type introduced in Swift 5.
此ClosureHTTP Manager使用Swift 5中引入的新Result類型。
The callback can be used from a closure, which is then used to update the UI. We may not be on the main thread after using the API call, so we need to make sure any work on the UI is on the proper thread.
可以從閉包中使用該回調,然后該閉包用于更新UI。 使用API??調用后,我們可能不在主線程上,因此我們需要確保UI上的所有工作都在正確的線程上。
The delegate pattern should be a one-to-one relationship. Therefore if we want to have a number of view controllers who conform to the protocol this is probably not the correct solution.
委托模式應該是一對一的關系。 因此,如果我們希望有許多符合協議的視圖控制器,那么這可能不是正確的解決方案。
Method 4 — NSNotifications
方法4 — NSNotifications
A notification means that multiple views can listen to notifications when the data has been received.
通知意味著當接收到數據時,多個視圖可以偵聽通知。
We can name the notification within an extension.
我們可以在擴展名中命名通知。
and we set up listeners for the notifications
我們為通知設置了監聽器
and prepare to receive the notification
并準備接收通知
This is then supported by a NotificationHTTPManager:
然后,NotificationHTTPManager支持此功能:
Issues:
問題:
The various iterations of HTTPManager are solely responsible for carrying out the HTTP call. If you wish to decode the JSON too, more work needs to take place and where should it take place?
HTTPManager的各種迭代僅負責執行HTTP調用。 如果您也希望解碼JSON,則需要進行更多工作,并且應該在哪里進行?
A git link might help to make this all a little bit clearer.
git鏈接可能有助于使這一切更加清晰。
Want to get in contact? User the link here:
想聯系嗎? 在此處使用鏈接:
翻譯自: https://medium.com/swift-coding/mvc-in-swift-a9b1121ab6f0
mvc2 mvc
總結
以上是生活随笔為你收集整理的mvc2 mvc_迅捷的MVC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 沙发的种类及特点有哪些?
- 下一篇: linux防火墙(firewall、ip