iOS Development Agent && Protocol (supplemental)

Source: Internet
Author: User

I. Understanding of agreements and agents

Agreement:

A protocol is a list of method signatures in which several methods can be defined. Depending on the configuration, classes that adhere to this Protocol will implement several of the methods specified in this protocol.

Agent:

An agent is a concept that is difficult to define with a noun (if we can say that the agreement is actually a list of methods). It's more like a relationship, I'm going to do something, but I don't want to do it myself, I entrust someone else to do it for me. This time, the other person is my agent.



Second, the use of the agreement

When defining the protocol, the @required and @optional can be used to configure the methods and options that must be implemented to comply with this protocol. Such as:

@protocol mychoosedayviewdelegate<nsobject>  @required  - returnchooseday: (ID) Mychoosedayview;  @optional  - hidethisview;   -Changedateformatter: (nsstring*) formatString;  @end

In the defined protocol Mychoosedayviewdelegate, if the protocol is adhered to, it is necessary to implement the-Returnchooseday: method, which can be implemented according to the actual program requirements-Hidethisview and- Changedateformatter:.


At the same time, the protocol supports an extension of itself, such as:

@protocol mychoosetimeviewdelegate< mychoosedayviewdelegate>  - dosomething; @end  
// Mychoosetimeviewdelegate extended the Mychoosedayviewdelegate protocol, that is to say, if mychoosetimeviewdelegate is observed, It is also necessary to implement the methods in Mychoosedayviewdelegate (@required) using classification, which is to use <> to introduce the required protocols in the header file of the definition class, if multiple protocols are introduced, separated by ",", for example: <mychoosedayviewdelegate, uialertviewdelegate> // Todo:balabalabala ... @end

To check whether a class implements a protocol or whether a class implements a protocol, you can test it in the following ways

// get an object   Myclassview *myclassview = xxxxxxx;   // determine if the object implements the Mychoosedayviewdelegate protocol   if ([Myclassview conformtoprotocol: @protocol (mychoosedayviewdelegate)]) {  //TODO: Balabalabala   ... }  // Determine if an object implements a method using the   if([Myclassview Responsetoselector: @selector (xxxx)]) {  //  Todo:balabalabala ...}  


Agent
Proxies are often used in iOS development, and proxies are often used in conjunction with protocols. I repeat my understanding of him, I want to do something, but I do not want to do it myself, I entrust other people to help me do it.
For example, I was writing code in my office and suddenly found my mouth thirsty like a bottle of ice, but downstairs was a hassle and I didn't want to go downstairs. This time I think of the store can increase the price of goods 2 yuan on the outside to send home service, I call the shop, commissioned a canteen to send me a bottle of water up. At this time, the canteen is my agent, I commissioned him to help buy water and sent up. In this example, the shop is my agent, the goods to increase the price of 2 yuan to send home service is a commodity trading agreement (this name is certainly not the best, let's call it) in one of the methods.
do not feel trouble, we finally again, the kiosk to abide by the commodity trading agreement, I let the kiosk become my agent, instead I finished buying a bottle of Ice Pepsi thing.
In terms of iOS development, Uiviewcontroller wants to use a tableview that requires compliance with the Uitableviewdatasource,uitableviewdelegate 2 protocols, At the same time set TableView agent for itself, can complete the implementation of table.

Iv. Use of proxies and protocols
I use the code to describe it as much as possible, but first I need to do some context.
What I want to do: I want to customize a view that shows some specific information in my own project. It may be a combination of several controls, such as Uitextfield, Uiswitch, Uidatepicker, Balabalabala ... Let's call this view for myinformationview  . I used this view in a viewcontroller, for convenience, This viewcontroller is called Myrootviewcontroller. When using this myinformationview, I need to get and handle the Myrootviewcontroller immediately. This is the function I want to implement.
Next, is the time to start writing code:
1. First is a protocol

#import <Foundation/Foundation.h>  @class myinformationview;    @protocol myinformatioviewdelegate<NSObject>  @required  @returnValueToShow:( Myinformationview*) Myinformationview;    @end  

2.MyInformationView

#import <UIKit/UIKit.h>@interface Myinformationview:uiview @property (nonatomic, strong) ID<MyInformationViewDelegate>MyDelegate; -(ID) Initwith ...//methods for initializing views//The required properties and methods are added according to actual needs.@end #import"MyInformationView.h"@interface Myinformationview () @end @implementation myinformationview @synthesize mydelegate=_mydelegate; //Todo: Handle Some initialization methods and some other events//Changemyinformationdata is the data movement that can be caused by manipulating this view. It could be a selector change, a click button, and so on, but we don't want to handle it in this view because its changes may affect the controller that calls this view, so ...- (void) Changemyinformationdata: (ID) Sender {//TODO: Data changes ....    if([self.mydelegate respondstoselector: @selector (returnvaluetoshow:)]) {[Self.mydelegate Returnvaluetosho          W:self]; }} @end

3.ViewController
The code is not written here, and it is declared in the header file that this class complies with the Myinformationviewdelegate protocol. In the implementation file (. m), when calling Myinfomationview, set the proxy to itself,

Myinformation *myinfoview == self ; // the method required to implement in the Protocol -(void) Returnvaluetoshow: (myinformationview*) myinfoview  {     / /TODO: Do what you want to  do }  


This is a complete set of proxy and protocol usage. But in fact, agents and protocols can do more things, such as when the page switch (a page into the AA page) ... By passing in, make AA a agent, and operate it to facilitate the handling of some problems.

iOS Development Agent && Protocol (supplemental)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.