-----------------dog.h
#import <Foundation/Foundation.h>
@class Dog; //下面用到dog类,所以向前声明
// @class表示前向申明一个类
// 定义一个人和狗通讯的方式 protocol
@protocol DogBark <NSObject>
- (void) bark:(Dog *)thisDog count:(int)count;
@end
@protocol DogBark; //协议写在后面,所以向前声明
// @protocol表示 协议前向申明
@interface Dog : NSObject
{
NSTimer *timer;
int barkCount;
int _ID;
@property int ID;
@property (assign) id <DogBark> delegate;
@end
-------------------------- dog.m
#import "Dog.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
#import <Foundation/Foundation.h>
@class Dog; //下面用到dog类,所以向前声明
// @class表示前向申明一个类
// 定义一个人和狗通讯的方式 protocol
@protocol DogBark <NSObject>
- (void) bark:(Dog *)thisDog count:(int)count;
@end
@protocol DogBark; //协议写在后面,所以向前声明
// @protocol表示 协议前向申明
@interface Dog : NSObject
{
NSTimer *timer;
int barkCount;
int _ID;
id <DogBark> delegate; // ownner 狗的主人
这一句 " id <DogBark> delegate;"类似java里泛型声明一个接口"interface 接口"
delegate的 类型是实现protocol DogBark 类型
}@property int ID;
@property (assign) id <DogBark> delegate;
@end
-------------------------- dog.m
#import "Dog.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