【发布时间】:2011-09-06 01:22:22
【问题描述】:
我正在了解委托的工作原理,已经通过一些示例,但现在有了一个用于测试的基本应用程序,似乎我还没有得到它,
这是我的代码:
类定义协议> *.h
#import <Foundation/Foundation.h>
@protocol protoPerra <NSObject>
-(void) dimeTuNombre:(NSString *) s;
@end
@interface MyClassic : NSObject {
id<protoPerra> _delegate;
}
@property (assign) id<protoPerra> delegate;
@end
类实现协议> *.m
#import "MyClassic.h"
@implementation MyClassic
@synthesize delegate = _delegate;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
[[self delegate] dimeTuNombre:@"pinguete"];
}
return self;
}
-(void) dealloc {
[_delegate release];
_delegate = nil;
[super dealloc];
}
@end
类采用协议:
#import "MyClassic.h" @interface MainViewController : UIViewController (protoPerra)
.m
#import "MainViewController.h"
@implementation MainViewController
-(void) dimeTuNombre {
NSLog(@"ss9!! tkt");
}
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *lava = [[[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 20)] autorelease];
lava.text = @"lava ss9";
[self.view addSubview:lava];
MyClassic *pirr = [[[MyClassic alloc] init ] autorelease];
[pirr setDelegate:self];
}
-(void) dimeTuNombre:(NSString *)s {
NSLog(@"%@",s);
}
@end
那么在这个简单的示例中缺少什么以使其与我的代表一起工作?
非常感谢!
请注意,我使用了 [在收养类的 .h 中] 的 (),就好像我使用雪佛龙一样,代码消失了
【问题讨论】: