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,
here my code:
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
Class implementing protocol> *.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
Class Adopting protocol:
#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?
thanks a lot!
Please note i have used () insted of the <> [in the .h of the adopting class] as if i use the Chevrons, the code disappears
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
instead of just simple init.
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).
You could change your MyClassic constructor as follows:
Then instead of this:
You will do this:
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.