Running protocol methods from another class - Objective-C

658 views Asked by At

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.

1

There are 1 answers

0
Merlevede On

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!
}