Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2618382
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-08-06 13:35:43

What is a "delegate" in Objective C's iPhone development?
A delegate allows one object to send messages to another object when an event happens. For example, if you're downloading data from a web site asynchronously using the NSURLConnection class. NSURLConnection has three common delegates:
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error - (void)connectionDidFinishLoading:(NSURLConnection *)connection - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response One or more of these delegates will get called when NSURLConnection encounters a failure, finishes successfully, or received a response from the web site, respectively.
A delegate is a pointer to an object with a set of methods the delegate-holder knows how to call. In other words, it's a mechanism to enable specific callbacks from a later-created object.
good example is UIAlertView. You create a UIAlertView object to show a short message box to users, possibly giving them a choice with two buttons like "OK" and "Cancel". The UIAlertViewneeds a way to call you back, but it has no information of which object to call back and what method to call.
To solve this problem, you can send your self pointer to UIAlertView as a delegate object, and in exchange you agree (by declaring the UIAlertViewDelegate in your object's header file) to implement some methods that UIAlertView can call, such as alertView:clickedButtonAtIndex:.
Check out this post for a quick high-level intro to the delegate design pattern and other callback techniques.
References:

I try to elaborate it through simple program

Two Classes

Student.h

#import <Foundation/Foundation.h> @interface Student : NSObject @property (weak) id delegate; - (void) studentInfo; @end

Student.m

#import "Student.h" @implementation Student - (void) studentInfo { NSString *teacherName; if ([self.delegate respondsToSelector:@selector(teacherName)]) { teacherName = [self.delegate performSelector:@selector(teacherName)]; } NSLog(@"\n Student name is XYZ\n Teacher name is %@",teacherName); } @end

Teacher.h

#import <Foundation/Foundation.h> #import "Student.h> @interface Teacher: NSObject @property (strong,nonatomic) Student *student; - (NSString *) teacherName; - (id) initWithStudent:(Student *)student; @end

Teacher.m

#import "Teacher.h" @implementation Teacher - (NSString *) teacherName { return @"ABC"; } - (id) initWithStudent:(Student *)student { self = [ super init]; if (self) { self.student = student; self.student.delegate = self; } return self; } @end

main.m

#import <Foundation/Foundation.h> #import "Teacher.h" int main ( int argc, const char* argv[]) { @autoreleasepool { Student *student = [[Student alloc] init]; Teacher *teacher = [[Teacher alloc] initWithStudent:student]; [student studentInfo]; } return 0; }

EXPLANATION :::

  1. From main method when initWithStudent:student will execute

    1.1 Teacher's object's property 'student' will be assigned with student object.

    1.2 self.student.delegate = self

     means student object's delegate will points to teacher object
  2. From main method when [student studentInfo] will be called

    2.1 [self.delegate respondToSelector:@selector(teacherName)] Here delegate already points to teacher object so it can invoke 'teacherName' instance method.

    2.2 so [self.delegate performSelector:@selector(teacherName)] will execute easily.

It looks like Teacher object assign delegate to student object to call it's own method.

It is a relative idea, where we see that student object called 'teacherName' method but it is basically done by teacher object itself.

阅读(203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~