[ios] delegate用法

认为delegate 也是nsnotication一样的一种消息发送机制.

1.定义delegate

testA类的h文件

@protocol testDelegate;

1.@interface  testA:NSObject

@property(assign) id<testDelegate> mydelegate;

 @end

@protocel testDelegate<NSObject>

//要让别的类实现的方法

-(void) testDelegateFunc;

@end

调用delegate方法

testa.m文件

@implementation testA

@synthesize mydelegate;

//在某个地方你想调用的时候调用

if ([self.mydelegate respondsToSelector:@selector(testDelegateFunc)]) {
        [self.mydelegate testDelegateFunc];
    }

@end

下一步你让谁实现这个方法,你就让谁实现这个接口(delegate)

我让testb类实现接口

testb.h

#import "testa.h"

@interface testb:NSobject<testDelegate>

@end

testb.m文件 实现这个方法就行

viewdidload{

testa * a=[teata alloc]init];

a.mydelegate=self;

}

-(void) testDelegateFunc{

NSLog("%@",@"这是testa的方法,testb实现testa的方法,这就是delegate模式");

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值