Cocoa Touch's 3 kinds of communication mode delegate/target/notification

Source: Internet
Author: User

1. Delegate

In the cycle of running a cocoa touch class object, a point in time it will invoke some specified function of the specified class to accomplish what he wants to accomplish. This "specified class" is referred to as the delegate class for this class. The "Specify function" is a method that is defined in the Protocol (protocal).

For example, UITableView, in its operating cycle, invokes a series of functions of the delegate class to do its own initialization work. All we need to do is to specify its delegate class and implement the delegate method that UITableView will invoke in the delegate class. So how do I know what methods UITableView want to invoke? In fact, it is very simple, it is to invoke the method is defined in the Protocol class inside. As you can find in the documentation, the name of the Protocol class associated with UITableView is also prefaced with UITableView. The method defined here is a number of methods that UITableView to invoke during the run cycle.

2. Target

Classes that inherit from Uicontrol, such as UIButton, can set the selector function to invoke the target object when an event is triggered.

[UIButton  addTarget: target object name  action: function name forControlEvents: event Name]  

3. Notification

This allows an object to send messages to the message center, which may be marked with some state information. Other objects may listen to information in the Message center with certain status messages, thereby making the corresponding.

Nsnotification *ntf = [nsnotification notificationwithname:@"Chgvalue" Object: Self]; //declares a message with the name Chgvalue and the sender the object itself. Nsnotificationcenter *ntfcenter =[Nsnotificationcenter Defaultcenter];  [Ntfcenter postnotification:ntf]; //get a singleton in the message center and send a well-defined message to the message center. Nsnotificationcenter *ntfcenter =[Nsnotificationcenter Defaultcenter]; [Ntfcenter addobserver:self selector: @selector (getntf) Name:@"Chgvalue" Object: Self]; [Ntfcenter addobserver:self selector: @selector (GETNTF2) Name:@"Chgvalue" Object: Self]; //get a singleton of the message Center, which is the source of this object, and the message named Chgvalue is distributed to the getntf and GETNTF2 functions of this object. 

Cocoa Touch's 3 kinds of communication mode delegate/target/notification

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.