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 9061071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:23:37+00:00 2026-06-16T15:23:37+00:00

I am fairly new at programming (I have no education in programming – everything

  • 0

I am fairly new at programming (I have no education in programming – everything I know have been obtained by reading tutorials) and completely new at XCode and iOS developing. So far I understand the basics in developing an iOS app, but one thing I can’t figure out how works is delegates. I understand the idea behind using delegates, but I don’t know what I am doing wrong when trying to implement delegates. I have created a small example (Single-view application) to illustrate how I am implementing a custom delegate and I hope you can tell me what I am doing wrong.

I am using XCode 4.5.2, iOS6.0 with ARC enabled.

In the example I create a simple NSObject subclass (TestClassWithDelegate). TestClassWithDelegate.h looks like this:

@protocol TestDelegate <NSObject>

-(void)stringToWrite:(NSString *)aString;

@end

@interface TestClassWithDelegate : NSObject

@property (weak, nonatomic) id<TestDelegate> delegate;

-(TestClassWithDelegate *)initWithString:(NSString *)theString;

@end

TestClassWithDelegate.m looks like this:

#import "TestClassWithDelegate.h"

@implementation TestClassWithDelegate

@synthesize delegate;

-(TestClassWithDelegate *)initWithString:(NSString *)theString
{
    self=[super init];

    [delegate stringToWrite:theString];

    return self;
}

@end

The view controller (ViewController) consists of a UILabel which I want to write some text to.
ViewController.h looks like this:

#import "TestClassWithDelegate.h"

@interface ViewController : UIViewController <TestDelegate>

@property (weak, nonatomic) IBOutlet UILabel *testlabel;

@end

ViewController.m looks like this:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize testlabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.testlabel.text = @"Before delegate";
    TestClassWithDelegate *dummy = [[TestClassWithDelegate alloc]    initWithString:@"AfterDelegate"]; //This should init the TestClassWithDelegate which should "trigger" the stringToWrite method.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark Test delegate
- (void)stringToWrite:(NSString *)aString
{
    self.testlabel.text = aString;
}
@end

Problem with above example is that the label on the view only writes “Before delegate” where I want it to write “AfterDelegate”.

All help is much appreciated. Happy New Year.

  • 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-06-16T15:23:38+00:00Added an answer on June 16, 2026 at 3:23 pm

    You haven’t set the delegate anywhere, so it will be nil. You’d either need to initWithString:delegate: instead of initWithString: or (better) just create the object, set the delegate, and send the string separately.

    You may have made a (common) mistake whereby you assume that @synthesize actually creates an object within your code and assigns a value to it. It does not. It is a (now mostly redundant!) instruction to the compiler to create accessor methods for a property.

    Below is a slightly reworked example of your delegate class, and some example usage:

    .h file:

    @interface TestClassWithDelegate : NSObject
    
    @property (weak, nonatomic) id<TestDelegate> delegate;
    -(void)processString:(NSString*)string
    
    @end
    

    .m file:

    @implementation TestClassWithDelegate
    
    -(void)processString:(NSString *)theString
    {
       [delegate stringToWrite:theString];
    }
    
    @end
    

    Usage:

    TestClassWithDelegate *test = [TestClassWithDelegate new];
    [test processString:@"Hello!"]; // Nothing will happen, there is no delegate
    test.delegate = self;
    [test processString:@"Hello!"]; // Delegate method will be called.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very much new to programming and have been doing fairly well so far.
I'm fairly new to database programming in WinForms, and have been using BindingSource, DataSet,
I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to programming in general but have been working with Java for
I'm fairly new to Visual Studio (specifically Visual Basic .NET) but have been programming
I'm fairly new to iOS programming, but have a basic understanding of it. I
I am fairly new to iOS programming and have run into a problem I
I'm fairly new to Android programming and have been searching around for some pointers
I'm fairly new to programming in Objective-C. While I have been able to find
I am fairly new to programming and have been working on a program to

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.