0

I have three different classes, one of them is parsing xml from a certain website and the two other will be recieving the information from the class that is running the NSXMLParserDelegate protocol methods. My question is, how do i tell the class to run the protocol methods from another class? Or run every instance method or the whole class or something like that.

Any suggestions?

Edit: I'm going to parse xml information from a website when some certain view is active. To do this, i'm going to have a class that i'm going to send a message to, and tell it to run its methods from the xml parser protocol and send the value it recieves to the view that is present.

Forever a noob
  • 689
  • 1
  • 9
  • 16
  • So these other two classes will *both* be a delegate of the first? Doesn't sound right to me. – trojanfoe Feb 07 '14 at 15:58
  • 1
    I think you're missing some basic understanding on protocols and delegates. Could you explain a little more what you're trying to do? – Merlevede Feb 07 '14 at 16:06
  • Okay, is there any other way i could acquire this then? – Forever a noob Feb 07 '14 at 16:07
  • 1
    A protocol is just a way of documenting the available methods, separate from the class definition. And a delegate is just an object you call to ask for information or obtain a service. Neither is performing any magic. – Hot Licks Feb 07 '14 at 18:00

1 Answers1

2

There are two ways of seeing it.

An object (A) having a pointer to the delegate (B) (the delegate is the object that implements the methods of a protocol) can call the methods of the protocol by just invoking them. Form the delegate's (B) point of view, you don't call the protocol's methods, you IMPLEMENT them, and some other object (A) will call them whenever it needs to inform you of some event, or to request some information. That's what protocols are designed for.

Object (A) somewhere it declares the delegate

id <someKindOfDelegate> delegate;

and whenever it want's, it calls the protocol's methods

if (self.delegate)
    [self.delegate someMethod]

(B) must declare itself as an implementor of the protocol

@interface ObjectB <someKindOfDelegate>

then (B) sets itself as the delegate of an instance of (A)

ObjectA *object = [[ObjectA alloc] init];
object.delegate = self;

and finally (B) implements the protocol's methods

- (void)someMethod {
     // do something... I've been called!
}
Merlevede
  • 8,140
  • 1
  • 24
  • 39