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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:30:05+00:00 2026-05-21T11:30:05+00:00

On iOS, how do I create a delegate (user defined)?

  • 0

On iOS, how do I create a delegate (user defined)?

  • 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-21T11:30:06+00:00Added an answer on May 21, 2026 at 11:30 am

    First define a declare a delegate like this –

    @protocol IconDownloaderDelegate;
    

    Then create a delegate object like this –

    @interface IconDownloader : NSObject
    {
        NSIndexPath *indexPathInTableView;
        id <IconDownloaderDelegate> delegate;
        NSMutableData *activeDownload;
        NSURLConnection *imageConnection;
    }
    

    Declare a property for it –

    @property (nonatomic, assign) id <IconDownloaderDelegate> delegate;
    

    Define it –

    @protocol IconDownloaderDelegate 
    
    - (void)appImageDidLoad:(NSIndexPath *)indexPath;
    
    @end
    

    Then you can call methods on this delegate –

    [delegate appImageDidLoad:self.indexPathInTableView];
    

    Here is the complete source code of the image downloader class –

    .h file –

    @class AppRecord;
    @class RootViewController;
    
    @protocol IconDownloaderDelegate;
    
    @interface IconDownloader : NSObject
    {
        AppRecord *appRecord;
        NSIndexPath *indexPathInTableView;
        id <IconDownloaderDelegate> delegate;
    
        NSMutableData *activeDownload;
        NSURLConnection *imageConnection;
    }
    
    @property (nonatomic, retain) AppRecord *appRecord;
    @property (nonatomic, retain) NSIndexPath *indexPathInTableView;
    @property (nonatomic, assign) id <IconDownloaderDelegate> delegate;
    
    @property (nonatomic, retain) NSMutableData *activeDownload;
    @property (nonatomic, retain) NSURLConnection *imageConnection;
    
    - (void)startDownload;
    - (void)cancelDownload;
    
    @end
    
    @protocol IconDownloaderDelegate 
    
    - (void)appImageDidLoad:(NSIndexPath *)indexPath;
    
    @end
    

    .m file –

    #import "IconDownloader.h"
    #import "MixtapeInfo.h"
    
    #define kAppIconHeight 48
    #define TMP NSTemporaryDirectory()
    
    @implementation IconDownloader
    
    @synthesize appRecord;
    @synthesize indexPathInTableView;
    @synthesize delegate;
    @synthesize activeDownload;
    @synthesize imageConnection;
    
    #pragma mark
    
    - (void)dealloc
    {
        [appRecord release];
        [indexPathInTableView release];
    
        [activeDownload release];
    
        [imageConnection cancel];
        [imageConnection release];
    
        [super dealloc];
    }
    
    - (void)startDownload
    {
        self.activeDownload = [NSMutableData data];
    
        // alloc+init and start an NSURLConnection; release on completion/failure
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                                 [NSURLRequest requestWithURL:
                                  [NSURL URLWithString:appRecord.mixtape_image]] delegate:self];
        self.imageConnection = conn;
        [conn release];
    
    }
    
    - (void)cancelDownload
    {
        [self.imageConnection cancel];
        self.imageConnection = nil;
        self.activeDownload = nil;
    }
    
    
    #pragma mark -
    #pragma mark Download support (NSURLConnectionDelegate)
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [self.activeDownload appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        // Clear the activeDownload property to allow later attempts
        self.activeDownload = nil;
    
        // Release the connection now that it's finished
        self.imageConnection = nil;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {   
        // Set appIcon and clear temporary data/image
        UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
        self.appRecord.mixtape_image_obj = image;
    
        self.activeDownload = nil;
        [image release];
    
        // Release the connection now that it's finished
        self.imageConnection = nil;
    
        // call our delegate and tell it that our icon is ready for display
        [delegate appImageDidLoad:self.indexPathInTableView];
    }
    
    @end
    

    and here is how we use it –

    #import "IconDownloader.h"
    
    @interface RootViewController : UITableViewController <UIScrollViewDelegate, IconDownloaderDelegate>
    {
        NSArray *entries;   // the main data model for our UITableView
        NSMutableDictionary *imageDownloadsInProgress;  // the set of IconDownloader objects for each app
    }
    

    in .m file –

    - (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath
    {
        IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];
        if (iconDownloader == nil) 
        {
            iconDownloader = [[IconDownloader alloc] init];
            iconDownloader.appRecord = appRecord;
            iconDownloader.indexPathInTableView = indexPath;
            iconDownloader.delegate = self;
            [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
            [iconDownloader startDownload];
            [iconDownloader release];   
        }
    }
    

    here is delegate gets called automatically –

    // called by our ImageDownloader when an icon is ready to be displayed
    - (void)appImageDidLoad:(NSIndexPath *)indexPath
    {
        IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];
        if (iconDownloader != nil)
        {
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:iconDownloader.indexPathInTableView];
    
            // Display the newly loaded image
            cell.imageView.image = iconDownloader.appRecord.appIcon;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I create a png image from text in iOS?
I want to create a custom alert view within my iOS application. For example,
Can I create an iOS commercial application (a couple of dollars to download in
I'm writing an iOS app with a table view inside a tab view. In
I am developing an iOS application, and trying to zip the file I have
How do I detect when an iOS app is launched for the first time?
I'm trying to drag a CALayer in an iOS app. As soon as I
What is the best way to generate random numbers using Objective-C on iOS? If
I'm diving into iOS development and am getting familiar with navigation view controllers. I'm
Suppose I create a file for writing like this: std::ofstream my_file(filename, std::ios_base::out | std::ios_base::trunc);

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.