太难了!对OC中协议和委托理解
我说很难是对我这个初学者来说的,哈哈。看了几天OC 的委托,查了些资料还是很迷糊,不知下面这种理解是否正确。请指点
#import <Foundation/Foundation.h>#import "TView.h"#import "TActive.h"int main(int argc, const char * argv[]){ @autoreleasepool { TView * v = [[TView alloc] init]; TActive * a = [[TActive alloc] init]; a.tView = v; v.Delegate = a; [v onClick]; } return 0;}TView.h文件:@protocol Message <NSObject> -(void) onClickMsg;@end@interface TView : NSObject <Message>{ NSString * TViewText; id <Message> Delegate; }@property (nonatomic,retain) id <Message> Delegate;@property (nonatomic,retain) TView * TviewText;- (void) onClick;- (TView *) init;- (void) Show;@endTView.m文件:#import "TView.h"@implementation TView@synthesize Delegate;@synthesize TviewText;-(TView *)init{ self = [super init]; if(self) { NSLog(@"上网请点击我"); } return self;}-(void)onClick{ NSLog(@"按键被点击。"); [Delegate onClickMsg]; }-(void) Show{ NSLog(@"已从网路上下载了数据。我将显示它"); NSLog(@"从网路上下载的数据是:%@",TviewText); NSLog(@"显示完毕谢谢使用。再见!");}@endTActive.h文件#import <Foundation/Foundation.h>#import "TView.h"@interface TActive : NSObject <Message>{ TView * tView;}@property (nonatomic,retain) TView * tView;@endTActive.m文件#import "TActive.h"@implementation TActive@synthesize tView;-(void) onClickMsg{ NSLog(@"从网路上下载数据。"); NSLog(@"下载的数据是:中国"); [tView setTviewText:@"中国"]; NSLog(@"下载完毕。"); [tView Show];}@end