关闭

协议,委托与代理

标签: 协议委托代理双向协议正向传值
353人阅读 评论(0) 收藏 举报
分类:

一.协议

1.定义:

OC或者iOS中的协议:一组方法声明的列表,协议中不需要实现这些方法,谁遵守这个协议,谁就需要实现协议中的方法

2.协议创建的步骤

  • 制定协议的内容(方法声明列表)
  • 遵守协议(.h)
  • 实现协议中的方法(.m)
  • 调用协议中的方法

3.声明协议的关键字

(1)@required

表示必须实现的方法(默认)

(2)@optional

表示可以实现也可以不实现的方法

4.双向协议

程序举例
(1)ProtocolManToWoman.h文件

#import <Foundation/Foundation.h>

@protocol ProtocolManToWoman <NSObject>//基协议

- (void)white;
- (void)rich;
- (void)beautiful;

@optional
- (void)writeCode;

@end

(2)ProtocolWomanToMan.h文件

#import <Foundation/Foundation.h>

@protocol ProtocolWomanToMan <NSObject>

@required
- (void)height;
- (void)rich;
- (void)handsome;

@optional
- (void)writeCode;

@end

(3)Man.h文件

#import <Foundation/Foundation.h>
#import "ProtocolToMan.h"
@interface Man : NSObject <ProtocolToMan>

- (void)height;
- (void)rich;
- (void)handsome;
//- (void)writeCode;
@end

(4)Man.m文件

#import "Man.h"

@implementation Man

- (void)height{
    NSLog(@"I am so high....");
}

- (void)rich{
    NSLog(@"I am so rich....");
}

- (void)handsome{
    NSLog(@"I am very handsome...");
}

//- (void)writeCode{
//    NSLog(@"write hello world!....");
//}

@end

(5)Woman.h文件

#import <Foundation/Foundation.h>
#import "ProtocolToWoman.h"
@interface Woman : NSObject <ProtocolToWoman>

- (void)white;
- (void)rich;
- (void)beautiful;

//- (void)writeCode;

@end

(6)Woman.m文件

#import "Woman.h"

@implementation Woman

- (void)white{
    NSLog(@"white.....");
}

- (void)rich{
    NSLog(@"rich.....");
}

- (void)beautiful{
    NSLog(@"beautiful......");
}

//- (void)writeCode{
//    NSLog(@"writting code....");
//}
@end

(7)main文件

#import <Foundation/Foundation.h>
#import "Man.h"
#import "Woman.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Man *man = [[Man alloc]init];
        if ([man respondsToSelector:@selector(height)]) {
            [man height];
        }
        if ([man respondsToSelector:@selector(rich)]) {
            [man rich];
        }
        if ([man respondsToSelector:@selector(handsome)]) {
            [man handsome];
        }
        if ([man respondsToSelector:@selector(writeCode)]) {
            [man writeCode];
        }
        else{
            NSLog(@"can not write code");
        }

        Woman *woman = [[Woman alloc]init];
        if ([woman respondsToSelector:@selector(white)]) {
            [woman white];
        }
        if ([woman respondsToSelector:@selector(rich)]) {
            [woman rich];
        }
        if ([woman respondsToSelector:@selector(beautiful)]) {
            [woman beautiful];
        }
        if ([woman respondsToSelector:@selector(writeCode)]) {
            [woman writeCode];
        }

    }
    return 0;
}

二.委托与代理

委托代理与正向传值程序举例
(1)ProtocolBossToWorker.h文件

#import <Foundation/Foundation.h>

@protocol ProtocolBossToWorker <NSObject>

- (void)driveCar:(int)time;

@end

(2)ProtocolWorkerToBoss文件

#import <Foundation/Foundation.h>

@protocol ProtocolWorkerToBoss <NSObject>

- (void)payMoney:(int)money;

@end

(3)Boss.h文件

#import <Foundation/Foundation.h>
#import "ProtocolBossToWorker.h"
#import "ProtocolWorkerToBoss.h"
@interface Boss : NSObject<ProtocolWorkerToBoss>

@property (retain,nonatomic)id <ProtocolBossToWorker>agent;
@property (assign,nonatomic)int timeOfDrive;

- (void)payMoney:(int)money;

- (void)getDrive;

@end

(4)Boss.m文件

#import "Boss.h"

@implementation Boss

- (void)payMoney:(int)money{
    NSLog(@"boss pay the money %d",money);
}

- (void)getDrive{
    if ([self.agent respondsToSelector:@selector(driveCar:)]) {
        [self.agent driveCar:_timeOfDrive];
    }
}

@end

(5)Worker.h文件

#import <Foundation/Foundation.h>
#import "ProtocolBossToWorker.h"
#import "ProtocolWorkerToBoss.h"
@interface Worker : NSObject<ProtocolBossToWorker>

@property (retain,nonatomic)id <ProtocolWorkerToBoss>agent;
@property (assign,nonatomic)int money;

- (void)driveCar:(int)time;

- (void)getPay;
@end

(6)Worker.m文件

#import "Worker.h"

@implementation Worker

- (void)driveCar:(int)time{
    NSLog(@"drive for %d hours",time);
    _money = time*5;
}

- (void)getPay{
    if ([self.agent respondsToSelector:@selector(payMoney:)]) {
        [self.agent payMoney:_money];
//        [self.agent payMoney:[self driveCar:self.boss.timeOfDrive]];
    }
}
@end

(7)main.m文件

#import <Foundation/Foundation.h>
#import "Boss.h"
#import "Worker.h"

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

    @autoreleasepool {
        Boss *boss = [[Boss alloc]init];
        Worker *worker = [[Worker alloc]init];
        boss.agent = worker;
        boss.timeOfDrive = 100;
        worker.agent = boss;
        [boss getDrive];
        [worker getPay];
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:54958次
    • 积分:1151
    • 等级:
    • 排名:千里之外
    • 原创:114篇
    • 转载:6篇
    • 译文:1篇
    • 评论:28条
    友情链接
    博客专栏
    最新评论