Reputation: 2488
I'm just trying to build a simple delegate, it compiles successfully but it doesn't seem to do anything right now. Please review my code below, both FirstViewContoller and SecondViewController have separate xib files, the SecondViewController has a button hooked up to the myMethod of course.
I've been learning my way around objective c and iPhone SDK for a couple of months, I've been using a book and lynda.com tutorial to aid, I actually copied most of the code using the xcode template for a utility app, and I believe I understand what is going on here, but is there something I've missed, as the button isn't responding.
If you need more info please ask.
Many thanks, Chris
SecondViewController.h
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate;
@interface SecondViewController : UIViewController {
id <SecondViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;
-(IBAction) myMethod;
@end
@protocol SecondViewControllerDelegate
-(void)viewControllerDidFinish:(SecondViewController *)controller;
@end
SecondViewController.m
@implementation SecondViewController
@synthesize delegate;
-(IBAction) myMethod
{
[self.delegate viewControllerDidFinish:self];
}
//
@end
FirstViewController.h
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>{
//
@end
FirstViewController.m
@implementation FirstViewController
-(void)viewControllerDidFinish:(SecondViewControllerDelegate *)controller
{
NSLog(@"delegate is being used");
}
//
@end
Edit - This is an old post but just to clarify I simply forgot to assign the SecondViewController delegate in the FirstViewController
Upvotes: 1
Views: 2249
Reputation: 1623
How do you assign the delegate property of SecondViewController? This is the last part I am missing...
Upvotes: 1
Reputation: 70998
Two things:
Your IBAction method myMethod
, if it really is an IBAction, needs to take an argument:
-(IBAction)myMethod:(id)sender
Before you blame the delegate pattern, make sure that myMethod is being called (set a breakpoint and see).
Your delegate method seems to be declared to take a delegate type as a param, not a viewController which is what you seem to want to be passing there. That might all get obscured by the id types, but I think you probably want to rejigger the declarations.
Upvotes: 0