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

  • Home
  • SEARCH
  • 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 6924897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:15+00:00 2026-05-27T10:43:15+00:00

I am trying to learn, how to implement delegation pattern in objective C. But

  • 0

I am trying to learn, how to implement delegation pattern in objective C. But the discussion almost exclusively concentrates on the adoption of protocols and then implementing the delegate methods that come with particular protocol – or – the delegation principle alone – or protocols alone.

What I am unable to find, is a easy to understand material about how to write a class that will serve as a delegator. By that I mean the class, which the message of some event will come from and which will provide the protocol for receiving that message – kind of 2in1 description. (protocols and delegation).

For the purpose of my learning, I’d like to go along the following trivial example, using an iPhone, a Cocoa touch application and Xcode4.2, using ARC, no Storyboard or NIBs.

Let’s have a class with name “Delegator”, which is a subclass of NSObject. The Delegator class has NSString instance variable named “report” and adopts the UIAccelerometerDelegate protocol.In the Delegator implementation, I will implement the the delegate method

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 

This delegate method will create a NSString @”myReport” and store it in the report variable anytime there is an accelerometer event. Further, I want to have a second class named ReportsStorage (a subclass of NSobject), which can store some Nsstring (report) in its instance variable called latestReport.
So far so good.
Now lets get back to theDelegator Class. I’d like to implement a protocol in Delegator named ReportsDelegate which will notify the class that adopts it (the ReportsStorage class), that a report was generated and will pass this report through the delegate method, which should be (I believe) something like this

-(void)delegator:(Delegator *)delegator didCreateNewReport:(NSString *)report;

Can you please provide the code for Delegator Class (incl. the “delegate” property), that will achieve this, with a description what each line of code means?

Thanks in advance, EarlGrey

  • 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-27T10:43:16+00:00Added an answer on May 27, 2026 at 10:43 am

    You’ll need to declare the delegate property as an id<ReportsDelegate> type. That is, any object type (id) conforming to the ReportsDelegate protocol (<ReportsDelegate>). Then, if the delegate method is considered optional, check if the delegate responds to that selector before calling it. (respondsToSelector:).

    Like so:

    Delegator.h

    #import <Foundation/Foundation.h>
    
    // Provide a forward declaration of the "Delegator" class, so that we can use
    // the class name in the protocol declaration.
    @class Delegator;
    
    // Declare a new protocol named ReportsDelegate, with a single optional method.
    // This protocol conforms to the <NSObject> protocol
    @protocol ReportsDelegate <NSObject>
    @optional
    -(void)delegator:(Delegator *)delegator didCreateNewReport:(NSString *)report;
    @end
    
    // Declare the actual Delegator class, which has a single property called 'delegate'
    // The 'delegate' property is of any object type, so long as it conforms to the
    // 'ReportsDelegate' protocol
    @interface Delegator : NSObject
    @property (weak) id<ReportsDelegate> delegate;
    @end
    

    Delegator.m

    #import "Delegator.h"
    
    @implementation Delegator
    @synthesize delegate;
    
    // Override -init, etc. as needed here.
    
    - (void)generateNewReportWithData:(NSDictionary *)someData {
    
        // Obviously, your report generation is likely more complex than this.
        // But for purposes of an example, this works.
        NSString *theNewReport = [someData description];
    
        // Since our delegate method is declared as optional, check whether the delegate
        // implements it before blindly calling the method.
        if ([self.delegate respondsToSelector:@selector(delegator:didCreateNewReport:)]) {
            [self.delegate delegator:self didCreateNewReport:theNewReport];
        }
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn how to implement an asynchronous pattern to query multiple
I am trying to learn Ruby, and want to implement the Python algorithms from
I am trying to learn Java. I would like to implement a simple networked
Trying to learn MVP pattern with C#... Does anyone know of any particularly good
I'm trying to learn Java EE 6 and i'm just wondering how to implement
I'm trying to learn Parsec by implementing a small regular expression parser. In BNF,
I'm just starting to learn Objective-C, one thing I'm trying to learn is good
While trying to learn c++, I tried to implement class representing very basic trie.
I am new to design patterns, but I have been trying hard to implement
I am trying to learn how to implement Web Services using Axis2 and Tomcat,

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.