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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:42:05+00:00 2026-06-05T06:42:05+00:00

I have tried going through this Question: How Does A Delegate Work and I

  • 0

I have tried going through this Question: How Does A Delegate Work and I still don’t seem to have a full grasp on it. I am trying to use the CocoaAsyncSocket library to create a TCP socket connection. Thanks to help from a very friendly SO user, I have the following code to perform a read data request to the server:

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
    NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
    if(msg)
    {
        NSLog(@"RX:%@",msg);
    }
}

Now, forgive my ignorance as I’m pretty new to iOS development. So now I have this method that I want to invoke which will perform my ReadData. The problem is, I do not know WHERE to put this method (I have several views, with several header/implementation files). I want this method to be a delegate method, but I do not know how to make it a delegate method. I want to invoke this delegate method from my view.

If anyone could explain:

  • Where do I put this code? (What file, etc)
  • How do I make this a delegate method?
  • How do I invoke this delegate method?

I’ve been stuck on this all day, and I’m about to throw it the towel lol. Any and all help is much appreciated. Thanks so much!

EDIT:

This is kind of a bridge from a previous question, but I don’t think that question has too much relevance to this question. Question

  • 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-05T06:42:08+00:00Added an answer on June 5, 2026 at 6:42 am

    Thanks for updating. It is now clearer.
    Here are some answers. If it is not clear, please let me know.

    – Where do I put this code? (What file, etc)

    This is a delegate method of CocoaAsyncSocket. Back to your first question, when you initialized it, you set yourself (your appDelegate) as the delegate.

    socket = [[AsyncSocket alloc] initWithDelegate:self];
    

    That means, you will be called from another class. So that means this method should be in the same class where you initialized the object (here socket) and set it as the delegate. So it stays in appDelegate

    – How do I make this a delegate method?

    You don’t. This is a delegate method itself.

    – How do I invoke this delegate method?

    You don’t. Another class (here AsyncSocket) will invoke it.

    You may now ask, how you pass the data to your viewControllers?
    That depends on your design. Once this method is called and you get notified that there is a connection, and data is being read, depending on your design, you pass the data to other view controllers. One way is by using NSNotification. e.g.

    // Call this in onSocket:didReadData:withTag: instead of logging
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataIsReadNotification" object:msg]
    
    
    
    // In one of your view controllers
    // View controllers insterested in this message, register to get notified:
    // add to -viewDidLoad
    [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateViewWithNotification:) name:@"DataIsReadNotification" object:nil];
    ...
    // and somewhere in the view controller class implement this
    - (void)updateViewWithNotification:(NSNotification *)notification {
    NSString *msg = [notification object];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with this all day and have tried going through the
I'm going crazy with this...why won't this center I have tried almost all the
I have been trying to make this work for a long time now. In
Suppose $2 is my variable. I have tried going from awk -F\, '{print $2
Have tried to find solutions for this and can't really come up with anything.
I have tried a lot to get this done but I dont know how
I have tried multiple date formats, but still I get nil back: Thu May
I have tried finding an answer to this problem and in every post I
I have tried posting this earlier and had to delete it because the code
I know this question has been asked several times, but I can't quite seem

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.