iOS in the delegation and agreement is a very important piece, if the understanding is not very difficult to distinguish between the agreement and commissioned what is different, these things are often encountered in the development;
A protocol is a set of interface functions that a class leaves to the outside (a master uses C + + base class to explain that the interface as a parameter, callback function of the base class, runtime recognition, call the corresponding subclass of the member function);
A delegate is a design pattern of iOS that invokes a method in a protocol by delegating another class, which is equivalent to a callback process;
Use understanding Delegation and protocol steps:
(1) Find out who entrusted who, what needs to do?
Take a blog dropdown refresh source case analysis, (in the Source Project Comment folder) There is a messagesystemview such a class, the class for the user's information used (such as who gave you a message, or commented on your blog), we also need to refresh to get the latest information, This feature is implemented using the drop down Refresh third party class library Egorefreshtableheaderview class delegate Messagesystemview class. This requires defining a protocol object delegate in the Egorefreshtableheaderview class.
(2) The entrusted class needs to declare the <XXDELEGATE> in the interface, indicating that the class is to implement the method in the protocol.
Messagesystemview implementation of Egorefreshtableheaderview protocol approach is to follow the Egorefreshtableheaderdelegate protocol.
(3) in the entrusted class to define a delegate class object, will be xx.delegate=self, meaning that the object of the delegate only want to be entrusted to the object;
In the EGORefreshTableHeaderView.h file
Egorefreshtableheaderview*_refreshheaderview;
In viewdidload view.delegate = self; _refreshheaderview = view;
-(void) viewdidload {allcount = 0;
[Super Viewdidload];
if (self.tabtitle) {self.tabBarItem.title = Self.tabtitle;
}//loading fixed data imagedownloadsinprogress = [Nsmutabledictionary dictionary];
comments = [[Nsmutablearray alloc] initwithcapacity:10];
[Self reload:yes]; Added code if (_refreshheaderview = = nil) {<span style= "color: #ff0000;" > Egorefreshtableheaderview *view = [[Egorefreshtableheaderview alloc] Initwithframe:cgrectmake (0.0f, -320.0f,
Self.view.frame.size.width, 320)];
View.delegate = self;</span> [self.tablecomments Addsubview:view]; <span style= "color: #ff0000;"
>_refreshheaderview = view;</span>} [_refreshheaderview refreshlastupdateddate]; Self.tableComments.backgroundColor = [Uicolor colorwithred:248.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0]
; }
(4) Calling the Protocol method in the implementation file of the entrusted class, which is a process of realizing the delegate method;
Here Egorefreshtableheaderview class object is entrusted to Messagesystemview class object, and the Egorefreshtableheaderview class protocol is implemented in Messagesystemview.
MESSAGESYSTEMVIEW.M file Implementation Delegate method
-(void) Egorefreshtableheaderdidtriggerrefresh: (Egorefreshtableheaderview *) View
{
[self Reloadtableviewdatasource];
[Self refresh];
}
-(BOOL) egorefreshtableheaderdatasourceisloading: (Egorefreshtableheaderview *) View
{return
_reloading;
}
-(NSDate *) egorefreshtableheaderdatasourcelastupdated: (Egorefreshtableheaderview *) View
{return
[ NSDate date];
}