Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:18:38+00:00 2026-05-24T22:18:38+00:00

I’m having a hard time getting my head around delegates. I tried putting together

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:18:39+00:00Added an answer on May 24, 2026 at 10:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.