8、协议Delegate代理模式-OC

代理协议在iphone开发中用了很多,几乎每个项目中都有用到。

代理和被代理端,怎么进行数据交互。

(1)协议的具体用法

(2)如何实现代理

(3)代理2端如何通讯


8、协议Delegate代理模式-OC_第1张图片

事件方法由人来实现;狗中存放有人的类;

这里将信息汇报给主人;

定时器:狗每个1秒向人汇报。(初始化狗的时候就开始定时发送数据)

.m

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:selfselector:

@selector(updataTimer:) userInfo:nilrepeats:YES];

意思是:[self updateTimer:nil];

- (void) updataTimer:(id)arg

{

barkCount ++;

//通知狗的主人

[delegatebark:self count:barkCount];

}

//这个函数在人中

- (void) bark:(Dog *)thisDog count:(int)count

{


}

不让main函数退出:

while(1)

{

[[NSRunloopcurrentRunLoop] run];

}


存放狗的主人:

id delegate;//ownner 狗的主人

在人调用setDog方法时将,主人id存放进去。


定义一个人和狗通信的协议:

在狗中定义一个协议

@protocol DogBark <NSOject>

- (void) bark:(Dog *)thisDog Count:(int)count;

@end

id<DogBark> delegate;//ownner 狗的主人


在人中实现这个协议


狗的声明和协议的声明:

@class Dog;//表示前向声明

@protocol DogBark;//协议前向声明



(1)协议的具体用法[事件通知;c语音中叫:回调指针]

(2)如何实现代理

(3)代理2端如何通讯


定义协议

//
//  Dog.h
//  DogAndPerson
//
//  Created by ccy on 13-12-21.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "DogToPerson.h"

@interface Dog : NSObject <DogToPerson>
{
    int _dogID;
    id _delegate;//人的类
    int barkCount;
    NSTimer * timer;
}
@property int dogID;
@property (retain) id delegate;

@end

Dog调用协议:

//
//  DogToPerson.h
//  DogAndPerson
//
//  Created by ccy on 13-12-21.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"
@class Dog;//这里一定要记得声明

@protocol DogToPerson <NSObject>
@optional
- (void) dogBark:(Dog *) thisDog barkCount:(int) barkCount;

@end

//
//  Dog.m
//  DogAndPerson
//
//  Created by ccy on 13-12-21.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import "Dog.h"

@implementation Dog

@synthesize dogID = _dogID;
@synthesize delegate = _delegate;

- (id)init
{
    self = [super init];
    if (self) {
        self.dogID = 0;
        barkCount = 0;
        timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(hand:) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)hand:(id)arg
{
    NSLog(@"dog hand\n");
    barkCount++;
    [self.delegate dogBark:self barkCount:barkCount];
}

@end

person申明协议:

//
//  Person.h
//  DogAndPerson
//
//  Created by ccy on 13-12-21.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

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


@end

//
//  Person.m
//  DogAndPerson
//
//  Created by ccy on 13-12-21.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import "Person.h"

@implementation Person
@synthesize dog = _dog;

- (void)dogBark:(Dog *)thisDog barkCount:(int)barkCount
{
    NSLog(@"dogBark thisDogID:%d, barkCount:%d\n", thisDog.dogID, barkCount);
}

@end

main 执行程序

//
//  main.m
//  DogAndPerson
//
//  Created by ccy on 13-12-21.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        
        Person * xiaoLi = [[Person alloc] init];
        Dog * dog1 = [[Dog alloc] init];
        dog1.dogID = 1024;
        dog1.delegate = xiaoLi;
        
        xiaoLi.dog = dog1;
        
        [dog1 release];
        
        while (1)
        {
            [[NSRunLoop currentRunLoop] run];
        }
        [xiaoLi release];
    }
    return 0;
}

















你可能感兴趣的:(8、协议Delegate代理模式-OC)