我说很难是对我这个初学者来说的,哈哈。看了几天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;
@end
TView.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
#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;
@end
TView.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
解决方案 »
- 求助下想学下IOS~
- 程序出现错误dyld: Library not loaded: @executable_path/
- UITableView Grouped模式滑动内容时会触发重载事件,如何取消?
- iOS 7运行iOS 6的项目,下边出现白边
- 分享个学习网址给大家,一起学习
- IOS如何发送IGMP包
- BROptionsButton 修改 tabBar 上按钮背景的颜色
- iOS 获取本地视频缩略图
- 创建项目出现这个问题:clang: error: linker command failed with exit code 1
- iOS开发,关于cocoapod
- iphone4s越狱了,还能开发吗?
- empty project添加.xib文件。
2012-04-20 16:15:20.953 Day8[233:403] 上网请点击我
2012-04-20 16:15:20.989 Day8[233:403] 按键被点击。
2012-04-20 16:15:20.993 Day8[233:403] 从网路上下载数据。
2012-04-20 16:15:20.995 Day8[233:403] 下载的数据是:中国
2012-04-20 16:15:20.998 Day8[233:403] 下载完毕。
2012-04-20 16:15:21.001 Day8[233:403] 已从网路上下载了数据。我将显示它
2012-04-20 16:15:21.003 Day8[233:403] 从网路上下载的数据是:中国
2012-04-20 16:15:21.005 Day8[233:403] 显示完毕谢谢使用。再见!
就比如你老板接到一个公司的活儿,然后叫你们程序员去完成。
你千万别把它和协议弄混了。
http://blog.langyufei.com/?p=273
你看看吧。
这里不用加<Message>,之所以用到协议,一个是为了让程序看起来更容易理解,另一个是为了规范,比如协议中如果有@require的方法,那么实现协议的类就必须实现这个@require的方法,这样可以保证委托一定会干一些事情(因为你用到委托,肯定是想让他实现一些行为)
一般都是委托去实现协议,@interface TActive : NSObject <Message>,这里就是委托实现协议,而TView这个类是使用委托的类,所以不需要实现<Message>
其实对象在交互的过程无非承担二种角色,1.数据接收者,2.数据发送者。在“代理协议”这个模型中,数据发送者包含数据接收者的指针。
@interface MyWidget:BaseWidget {
id<BaseWidgetDelegate> delegate;
}
@property (assign) id <BaseWidgetDelegate> delegate;
@end而数据接收者则需要实现协议
@protocal BaseWidgetDelegate;
@interface WidgetManager:NSObject<BaseWidgetDelegate>
....
@end