iphone - Creating/Invoking Objective-C Delegate Methods Objective-C -


i have tried going through question: how delegate work , still don't seem have full grasp on it. trying use cocoaasyncsocket library create tcp socket connection. friendly user, have following code perform read data request 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 ignorance i'm pretty new ios development. have method want invoke perform readdata. problem is, not know put method (i have several views, several header/implementation files). want method delegate method, not know how make delegate method. want invoke delegate method view.

if explain:

  • where put code? (what file, etc)
  • how make delegate method?
  • how invoke delegate method?

i've been stuck on day, , i'm throw towel lol. , appreciated. much!

edit:

this kind of bridge previous question, don't think question has relevance question. question

thanks updating. clearer. here answers. if not clear, please let me know.

- put code? (what file, etc)

this delegate method of cocoaasyncsocket. first question, when initialized it, set (your appdelegate) delegate.

socket = [[asyncsocket alloc] initwithdelegate:self]; 

that means, you called class. means method should in same class initialized object (here socket) , set delegate. stays in appdelegate

- how make delegate method?

you don't. delegate method itself.

- how invoke delegate method?

you don't. class (here asyncsocket) invoke it.

you may ask, how pass data viewcontrollers? depends on design. once method called , notified there connection, , data being read, depending on design, pass data other view controllers. 1 way using nsnotification. e.g.

// call in onsocket:didreaddata:withtag: instead of logging [[nsnotificationcenter defaultcenter] postnotificationname:@"dataisreadnotification" object:msg]    // in 1 of view controllers // view controllers insterested in message, register notified: // add -viewdidload [nsnotificationcenter defaultcenter] addobserver:self selector:@selector(updateviewwithnotification:) name:@"dataisreadnotification" object:nil]; ... // , somewhere in view controller class implement - (void)updateviewwithnotification:(nsnotification *)notification { nsstring *msg = [notification object]; } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -