The delegate mode evolves from Gof decoration mode, adapter mode and template method, and almost every application will use the delegate mode more or less.
In ancient Greece there was a philosopher who did only three things in his life: "Sleep", "eat", and "work". In order to better life, improve work efficiency, he will find a disciple, entrust these things to his brother to do. To become his apprentice, however, an agreement is required to achieve the problem of "sleeping", "eating" and "working". The relationship between the three is as follows.
As you can see, the generic class (philosopher) maintains a "weak reference" to the delegate object (Viewcontroller)
(id<philosopherdelegate> delegate), the principal object (Viewcontroller) is the philosopher's "Apprentice", which implements the agreement
philosopherdelegate. Philosopherdelegate prescribed 3 methods: Sleep, eat, work;
Code PhilosopherDelegate.h Code is as follows
@protocol philosopherdelegate@requied-(void) sleep;-(void) eat;-(void) work; @end
You can see that the Philosopherdelegate defines three methods. If the delegate agreement does not have m files. Its definition can be placed in other H files. He has to implement the delegate class Viewcontroller, the relevant code is as follows:
viewcontroller.h// philosopher//// Created by Chenhao on 10/30/14.// Copyright (c) xxteam. All rights reserved.//#import <UIKit/UIKit.h> #import "PhiosopherDelegate.h" #import "Phiosopher.h" @interface viewcontroller:uiviewcontroller<phiosopherdelegate> @end//// viewcontroller.m// Philosopher//// Created by Chenhao on 10/30/14.// Copyright (c) xxteam. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void ) Viewdidload { [super viewdidload]; Phiosopher *obj=[[phiosopher alloc] init]; obj.delegate=self; [obj start];} -(void) sleep{ NSLog (@ "Sleep");} -(void) eat{ NSLog (@ "eat");} -(void) work{ NSLog (@ "work");} @end
How does a delegate object establish a reference relationship in a generic class? We use the Obj.delegate=self statement in the Viewcontroller method to specify the reference relationship for the delegate object and the generic class structure. In half cases, the generic class is provided directly by the framework. In this example, we implement the generic class as needed.
The code is as follows
phiosopher.h// philosopher//// Created by Chenhao on 10/30/14.// Copyright (c) xxteam. All rights reserved.//#import <Foundation/Foundation.h> #import "PhiosopherDelegate.h" @interface Phiosopher: nsobject{ Nstimer *timer; int count;} @property (Nonatomic,weak) id<phiosopherdelegate> delegate;-(void) start;-(void) handle; @end
In the above code, we define the delegate property, its type food id "phiosopherdelegate", he can save a reference to the delegate object, where the property weak description is a weak reference, Here is a weak reference to prevent the memory reference count from increasing by two causing the delegate object to fail to release.
phiosopher.m// philosopher//// Created by Chenhao on 10/30/14.// Copyright (c) xxteam. All rights reserved.//#import "Phiosopher.h" @implementation phiosopher-(void) start{ count=0; Timer=[nstimer scheduledtimerwithtimeinterval:3.0 target:self selector: @selector (handle) Userinfo:nil Repeats:yes];} -(void) handle{ switch (count) {case 0: [self.delegate sleep]; count++; break; Case 1: [self.delegate eat]; count++; break; Case 2: [self.delegate work]; [Timer invalidate]; break; Default: Break ; } } @end
IOS development----delegation mechanism