views:

27

answers:

1

How can I call an Objective-C method from another class?

For instance in xCode I have two scripts : the delegate and another one. How might I call someFunction on script2 from the delegate?

Thanks! Christian Stewart

A: 

If by "script" you mean class, you would need to link both classes.

if objectA is delegate of objectB, when objectB call a method from the delegate protocol:

if ([self.delegate resposndsToSelector:@selector(classBdidSomething:)]) {
    [self.delegate performSelector:@selector(classBdidSomething:) withObject:self];
}

On the implementation of ClassA:

#pragma mark -
#pragma mark Delegate methods
- (void)classBdidSomething:(ClassB *)objectB {
    [objectB methodToBeCalled];
}

If you get an error/warning (like warning: no '-methodToBeCalled' method found) when building on the line:

[objectB methodToBeCalled];

It means that you have to import ClassB to into your ClassA, by doing:

#import "ClassB.h"
vfn
Sorry, I meant could I call something on ClassB from the delegate? SOrry to be confusing.
Christian Stewart
have you seen my answer. I call [objectB methodToBeCalled]; from ClassA, on the delegate method.
vfn