Reputation: 373
I'm having a hard time getting my head around delegates. I tried putting together a simple example to teach myself, but it's just not working.
My simple test has two classes. ClassA and ClassB. ClassA has the delegate definition. As well as a button that calls a method (-(void)buttonHasBeenPressed:(id)sender
) that contains the delegate method call (-(void)buttonPressed
). ClassB is set (I think) to listen to the ClassA delegate and when the -(void)buttonHasBeenPressed:(id)sender
is called it writes out to the console.
Here's the AppDelegate:
.h:
#import <UIKit/UIKit.h>
@interface DelegateTestAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
.m:
#import "DelegateTestAppDelegate.h"
#import "ClassA.h"
@implementation DelegateTestAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ClassA *classA = [[ClassA alloc] init];
[self.window addSubview:[classA view]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
@end
Here's ClassA:
.h:
#import <UIKit/UIKit.h>
@protocol ClassADelegate <NSObject>;
-(void)buttonPressed;
@end
@interface ClassA : UIViewController {
id <ClassADelegate> delegate;
}
@property (retain) id <ClassADelegate> delegate;
-(void)buttonHasBeenPressed:(id)sender;
@end
.m:
#import "ClassA.h"
@implementation ClassA
@synthesize delegate;
-(id)init {
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonHasBeenPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"hola" forState:UIControlStateNormal];
button.frame =CGRectMake(50, 200, 100, 20);
[self.view addSubview:button];
}
-(void)buttonHasBeenPressed:(id)sender {
[self.delegate buttonPressed];
NSLog(@"button has been pressed");
}
@end
Here's ClassB:
.h:
#import <UIKit/UIKit.h>
#import "ClassA.h"
@interface ClassB : UIViewController <ClassADelegate> {
}
@end
.m:
#import "ClassB.h"
@implementation ClassB
-(id)init {
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
-(void)buttonPressed {
NSLog(@"delegating!");
}
@end
The expected result is to have both "button has been pressed" and "delegating!" written to the console when clicking on the "hola" button.
Thanks for your help... Shawn
Upvotes: 0
Views: 545
Reputation: 2086
you forgot to set delegate to class which conforms to ClassADelegate protocol, here which should be an object of ClassB
Upvotes: 1
Reputation: 5120
Where is your ClassB's instance? I can't find it in your posted code. Also, where do you set the instance into ClassA as its delegate at? I can't find it neither.
If you are not doing this two things, your delegate will never be invoked.
Upvotes: 1