views:

295

answers:

1

Hi all,

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
A: 

Two things:

  1. 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).

  2. 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.

quixoto
Thanks for the reply Ben, it hasn't helped though1 - If I try to add a sender argument to an IBaction method I get an error in IB and the app crashes2 - That was a typo when transferring the code to this question, I've amended the above and that won't have been the case.Is there anything else it could be?
Chris
Thanks again Ben, please ignore as Adam's suggestion helped. Thanks for pointing out my mess though!
Chris
for the record, - (IBAction)respondToButtonClick;- (IBAction)respondToButtonClick:(id)sender;- (IBAction)respondToButtonClick:(id)sender forEvent:(UIEvent*)event;Are all valid IBActions, according to http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/IB_UserGuide/CodeIntegration/CodeIntegration.htmlIf you change the type of the IBAction, I think you have to re-wire the connection in IB otherwise it'll try to send the wrong selector
Kenny Winker
Cool, glad it's working. I was assuming that in the uncopied snippets you were setting the delegate. :)
quixoto
Ah, I will give that a shot, I'm not by my computer right now, but the error message on IB did say the action is no longer available, so I suppose re-wiring does make sense, still getting used to developing on mac but it's nice when I know what I'm doing! Thanks again
Chris