IOS development-Stage 3-Weibo (2) (Weibo MVC) and iosmvc
MVC design pattern
Design Model: Experience, methods, and tools summarized by the predecessors
Delegate, observer, Singleton Factory
Model-View-Controller
Design Patterns widely used in iOS development
The Model processes data or business logic.
View Interface between the program and the user. You can see its existence.
The Controller processes user interaction and transmits data and request between views and models.
Controller-View Controller-Model
There is no direct operation between View and Model.
View: webView
Model: request sina
Controller: ViewCtroller
The user clicks View and passes the user operation to the Controller. After the Controller updates the Model and the Model operation, the user notifies the Controller that I have set the Controller to refresh the UI.
Unlike the DAO design mode, DAO is actually equivalent to the Model function in MVC.
Authorization
Access_token
TimeLineViewController public Weibo
M: ZYSinaEngine is responsible for Sina-related and request modules.
V: ZYLoginWebView displays the authorization interface for user interaction.
C: ZYTimeLineViewController is responsible for managing the data between V and M and the transmission of requests between V and V.
Interaction between Class and Class/(Object and object): 1. Notification 2. Proxy (Protocol) 3. property: object to be created
Design Code according to MVC
New ZYLoginWebView inherits from UIView
Add the webView attribute in ZYLoginWebView. h.
Create the webView Initialization Method in ZYLoginWebView and add
Set the proxy import Protocol to implement two protocol Methods
Override the layoutSubviews method to set the webView frame
Let the webView load request be managed by M to create Model class ZYSinaEngine
Add the plus sign method to the ZYSinaEngine class (convenient) authorizeReuqest to return a request
The authorizeReuqest method requires many application parameters created by Sina and the parameters are used multiple times. Therefore, the macro definition is in ZYSinaEngine.
In ZYLoginWebView. h, define ***-(void) loadAuthorizeRequest :( NSURLRequest *) request;
Create and initialize ZYLoginWebView in TimeLineViewController and load the request
Receive code in webView proxy method
After receiving the code, you need to tell TimeLineViewController to use the Protocol.
Because ZYLoginWebView wants to transmit information to others, in ZYLoginWebView, there should be a delegate Object Property id <protocol name> delegate
Write the Protocol method in ZYLoginWebView and call the Protocol method in. m.
NSError Composition
TimeLineViewController implements the proxy method to get the code
Declare an instance method in ZYSinaEngine to request accesstoken based on the code
After obtaining the token, inform the Controller to obtain the accesstoken usage protocol.
Because ZYSinaEngine wants to transmit information to others, ZYSinaEngine should have a delegate Object Property id <protocol name> delegate
Write the Protocol method in ZYSinaEngine and use the proxy object to call the Protocol method in. m. Because the Protocol method is an optional method, you should check whether the proxy object exists and whether the proxy method can be used.
TimeLineViewController implements proxy method to get access_token
Declare an instance method in ZYSinaEngine to request public microblog Data Based on accesstoken
After the data is obtained, the resolution tells the Controller to use ZYSinaEngineDelegate to obtain the data.