objective-c协议代表 [英] objective-c protocol delegates
问题描述
我了解代表的工作原理,通过一些例子,但现在有一个基本的测试应用程序,似乎还没有得到它。
I'm understanding how delegates work, had gone trough some examples, but now with a basic app for testing, it seems i haven't yet got it,
这里我的代码:
类定义协议> * .h
Class defining protocol > *.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
Class implementing protocol> *.m
#importMyClassic.h
#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
类采用协议:
#importMyClassic.h
@interface MainViewController:UIViewController(protoPerra)
#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
所以在这个简单的例子中缺少什么它与我的代表一起工作?
so what is missing in this simple example to make it work with my delegate?
非常感谢!
请注意,我已经使用()
Please note i have used () insted of the <> [in the .h of the adopting class] as if i use the Chevrons, the code disappears
推荐答案
你打电话给在MyClassic init中的委托方法(dimeTuNombre :),但代理尚未设置,所以您需要重新思考什么时候调用dimeTuNombre:或重构MyClassic类以创建一个构造函数,如
You call the delegate method (dimeTuNombre:) in MyClassic init, but the delegate is not set yet, so you either need to rethink when to call dimeTuNombre:, or refactor the MyClassic class to have a constructor like
-(id)initWithDelegate:(id<protoPerra>)delegate
而不是简单的初始化。
要澄清,在当前的代码中,您可以在MainViewController.h中设置代理[pirr setDelegate:自]; - 将MyClassic属性的值delegate设置为MainViewController的当前实例。请注意,当您调用MyClassic的init时,代理尚未设置,因此[[self delegate] dimeTuNombre:@pinguete];调用不做任何事情(代理在这一点上为零)
To clarify, in your current code you set the delegate in MainViewController.h when you do [pirr setDelegate:self]; - which sets the value "delegate" of the property of MyClassic to the current instance of MainViewController. Note that when you call MyClassic's init, the delegate isn't set yet, so the [[self delegate] dimeTuNombre:@"pinguete"]; call does nothing (delegate is nil at that point).
您可以按照以下方式更改MyClassic构造函数:
You could change your MyClassic constructor as follows:
-(id)initWithDelegate:(id<protoPerra>)aDelegate
{
self = [super init];
if (self) {
_delegate = aDelegate;
// Initialization code here.
[[self delegate] dimeTuNombre:@"pinguete"];
}
return self;
}
而不是这样:
MyClassic *pirr = [[[MyClassic alloc] init ] autorelease];
[pirr setDelegate:self];
您将这样做:
MyClassic *pirr = [[[MyClassic alloc] initWithDelegate:self ] autorelease];
这将工作,但请注意,通常委托用于各种通知 - 如按钮报告它被点击或套接字说它已经收到一些数据。
This will work, but note that usually delegates are used for notifications of various kinds - like a button reporting it was clicked or a socket says it has received some data.
这篇关于objective-c协议代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!