利用协议实现回调函数
利用协议实现回调函数
[cpp] view plaincopyprint?
01.#import <UIKit/UIKit.h>
02.
03.@protocol myViewDelegate
04.
05.-(void) CallBackFun;
06.
07.@end
#import <UIKit/UIKit.h>
@protocol myViewDelegate
-(void) CallBackFun;
@end
调用协议:论文网
[cpp] view plaincopyprint?
01.#import <Foundation/Foundation.h>
02.#import "myViewDelegate.h"
03.
04.@interface Test : NSObject{
05. id<myViewDelegate> delegate;
06.}
07.
08.@property(nonatomic,retain) id<myViewDelegate> delegate;
09.
10.-(void)callback;
11.
12.@end
13.#import "Test.h"
14.
15.@implementation Test
16.@synthesize delegate;
17.
18.-(id)init{
19. NSLog(@"init!");
20. return [super init];
21.}
22.
23.-(void)callback
24.{
25. NSLog(@"callbackInTest!");
26. if (delegate!=nil) {
27. [delegate CallBackFun];
28. }
29.
30.}
31.@end
#import <Foundation/Foundation.h>
#import "myViewDelegate.h"
@interface Test : NSObject{
id<myViewDelegate> delegate;
}
@property(nonatomic,retain) id<myViewDelegate> delegate;
-(void)callback;
@end
#import "Test.h"
@implementation Test
@synthesize delegate;
-(id)init{
NSLog(@"init!");
return [super init];
}
-(void)callback
{
NSLog(@"callbackInTest!");
if (delegate!=nil) {
[delegate CallBackFun];
}
}
@end
实现协议,接受回调:
[cpp] view plaincopyprint?