转:http://tigercat1977.blog.163.com/blog/static/2141561122012111295321374/

第八讲:Obj-C 协议 Delegate 代理设计模式 


      代理设计模式 也叫做 委托设计模式
主要内容

      协议的具体用法
      如何实现代理
      代理两端如何通讯

案例 : 通过狗每隔一秒向主人叫一次                 类似C语言中的回调函数

   第八讲:Obj-C 协议 Delegate 代理设计模式 - tigercat1977 - tiger notes

通过狗每隔一秒向主人叫一次  

// Dog.h #import <Foundation/Foundation.h> @protocol DogBark; // 这里声明后边的 DogBark 协议 // @protocol 表示协议前向声明 // @class Dog; 也可以,把后面的 DogBark 协议放到前边,声明 Dog // @class 表示前向声明一个类 @interface Dog : NSObject { int _ID; NSTimer *timer; // 定时器 int barkCount; // 叫的次数 id <DogBark> delegate; //ownner 存狗的主人 } @property int ID; @property (assign) id <DogBark> delegate; @end // 定义一个人和狗通讯的方式 protocol @protocol DogBark <NSObject> - (void) bark:(Dog *)thisDog count:(int)count; @end

// Dog.m #import "Dog.h" #import "Person.h"

@implementation Dog @synthesize ID = _ID; @synthesize delegate; - (id) init { self = [super init]; if (self) { timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES]; // 创建一个定时器 每隔 1.0s 就调用 [self updateTimer:nil]; } return self; } - (void) updateTimer:(id)arg { barkCount++; NSLog(@"dog bark %d", barkCount); // 通知狗的主人 就是 delegate [delegate bark:self count:barkCount]; // 调用 delegate 主人里面的 bark:count: 方法 // 向主人回报 } @end


// Person.h #import <Foundation/Foundation.h> #import "Dog.h" @interface Person : NSObject <DogBark> { Dog *_dog; } @property (retain) Dog *dog; @end

// Person.m #import "Person.h" @implementation Person @synthesize dog = _dog; - (void) setDog:(Dog *)dog { if (_dog != dog) { [_dog release]; _dog = [dog retain]; // 通知 _dog 的主人是 self [_dog setDelegate:self]; } } - (Dog *) dog { return _dog; } - (void) bark:(Dog *)thisDog count:(int)count { // 当狗叫的时候来调用 xiaoLi 人的这个方法 NSLog(@"Person this dog %d bark %d", [thisDog ID], count); } - (void) dealloc { self.dog = nil; [super dealloc]; } @end


// main.m #import <Foundation/Foundation.h> #import "Dog.h" #import "Person.h" int main (int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Hello, World!"); Person *xiaoLi = [[Person alloc] init]; Dog *dog = [[Dog alloc] init]; [dog setID:10]; [xiaoLi setDog:dog]; [dog release]; while (1) { [[NSRunLoop currentRunLoop] run]; } [xiaoLi release]; } return 0; } /* 输出结果 2012-12-17 21:45:18.909 Hello, World! 2012-12-17 21:45:19.913 dog bark 1 2012-12-17 21:45:19.914 Person this dog 10 bark 1 2012-12-17 21:45:20.920 dog bark 2 2012-12-17 21:45:20.923 Person this dog 10 bark 2 2012-12-17 21:45:21.912 dog bark 3 2012-12-17 21:45:21.913 Person this dog 10 bark 3 2012-12-17 21:45:22.912 dog bark 4 2012-12-17 21:45:22.913 Person this dog 10 bark 4 2012-12-17 21:45:23.913 dog bark 5 2012-12-17 21:45:23.914 Person this dog 10 bark 5 2012-12-17 21:45:24.913 dog bark 6 2012-12-17 21:45:24.914 Person this dog 10 bark 6 2012-12-17 21:45:25.913 dog bark 7 2012-12-17 21:45:25.914 Person this dog 10 bark 7 ........................................... ....................... ............... */


(本讲整理完)

 

posted on 2013-03-07 16:59  jackljf  阅读(284)  评论(0编辑  收藏  举报