zoukankan      html  css  js  c++  java
  • 第八讲:ObjC 协议 Delegate 代理设计模式

    转: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 ........................................... ....................... ............... */


    (本讲整理完)
  • 相关阅读:
    数据结构与算法之PHP实现二叉树的遍历
    数据结构与算法之二叉树的基本概念和类型
    聚集索引,非聚集索引,覆盖索引 原理
    Vue学习笔记:methods、computed、watch的区别
    xsl 和xml transform方法的调用
    Chrome , Firfox 不支持fireEvent的方法
    分布式存储
    firefox并不支持selectSingleNode和selectNodes的解决方法
    503 Service Unavailable
    处理【由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面】
  • 原文地址:https://www.cnblogs.com/jackljf/p/3589248.html
Copyright © 2011-2022 走看看