技术的乐趣在于分享,欢迎多多交流,多多沟通。
全部博文(877)
分类: iOS平台
2015-08-06 13:35:43
- (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.
A good example isUIAlertView
. You create aUIAlertView
object to show a short message box to users, possibly giving them a choice with two buttons like "OK" and "Cancel". TheUIAlertView
needs 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 yourself
pointer toUIAlertView
as a delegate object, and in exchange you agree (by declaring theUIAlertViewDelegate
in your object's header file) to implement some methods thatUIAlertView
can call, such asalertView: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 :::
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
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.