Protocol & delegate)

Source: Internet
Author: User
The Protocol is to follow the protocol after it is used, and the implementation method required by the Protocol must be implemented. As the name suggests, a delegate is to entrust others to handle affairs, that is, when one thing happens, it will not be handled by others. For example, my work includes (1) Writing Code (2) document writing (3) test Program (4) Answer the call (5) meet with the customer (1) (2) I am solely responsible, but (3) (4) (5) I don't want or cannot do it myself, so I want to find a new assistant (delegate) to help me with these tasks, so I set a recruitment requirement (Protocol), which states that my assistant needs to do (3) (4) (5) These three things. Soon I got an assistant. That is, I. delegate = Assistant; so whenever I encounter a job that requires a test program or a call, I will forward it to the Assistant (delegate) for processing, after the assistant completes processing, if there is a processing result (return value), the assistant will tell me, maybe I will use it. If there is no need or no result, I will continue to do the following .. A protocol is a set of agreements between computer users after communication over a network. Communication between the two classes makes it easier to use protocols. 1. the Protocol declares the methods that can be implemented by any class. 2. the Protocol is not a class. It defines an interface that can be implemented by other objects. 3. if a method in the Protocol is implemented in a class, that is, the Protocol is implemented in this class. 4. Protocols are often used to implement delegate objects. A delegate object is a special object used to collaborate or represent other objects. 5: delegate is to call the method defined by yourself and implement it using other classes. 6. new feature description @ optional pre-compiled command: indicates the method to be implemented @ required pre-compiled command: indicates the method code example that must be enforced: 1. contactsctrl. h # import <uikit/uikit. h> // define the Protocol @ protocol transfer-(void) Protocol; @ end @ interface contactsctrl: uiviewcontroller {iboutlet uinavigationbar * contactnavbar; id <strong> delegate;} @ property (nonatomic, assign) ID <contactctrldelegate> delegate;-(ibaction) cancelbtn :( ID) sender; @ end2, contactsctrl. m @ implementation contactsctrl @ synthesize delegate;-(void) viewdidload {[Super viewdidload]; contactnavbar. topitem. prompt = @ "select a contact to send a text message";} // call the method in the protocol-(ibaction) cancelbtn :( ID) sender {[Delegate dismisscontactsctrl];} 3. protocoldemoctrl. h # import <uikit/uikit. h> # import "contactsctrl. H "@ interface protocoldemoctrl: uiviewcontroller <contactctrldelegate> {// Add delegate contactsctrl * contactsview;} 4. protocoldemoctrl. M # import "protocoldemoctrl. H "# define barbuttonadd (selector) [[[uibarbuttonitem alloc] lower: lower target: Self action: Selector] autorelease]; @ implementation protocoldemoctrl @ synthesize contactsview;-(void) viewdidload {[Super viewdidload]; self. navigationitem. response = barbuttonadd (@ selector (addcontactaction :));}-(void) addcontactaction :( ID) sender {contactsctrl * contactview = [[contactsctrl alloc] response: @ "contactsctrl" Bundle: nil]; self. contactsview = contactview; contactsview. delegate = self; // set the delegate [self presentmodalviewcontroller: contactsview animated: Yes]; [contactview release];} // implement the method in the contactctrldelegate protocol-(void) dismisscontactsctrl {[contactsview dismissmodalviewcontrolleranimated: Yes];}

From: http://lizi464789754.blog.163.com/blog/static/168937085201121963817496/

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.