Why isn't my custom iPhone delegate working?

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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 

@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

profilepic.png
manpreet 2 years ago

How do you assign the delegate property of SecondViewController? This is the last part I am missing...


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.