there. I got 2 class
@implemention A
-(void) method1{
something;
}
@end
@implemention B
-(void) method2{
something
}
@end
if the A class' implement can't be modified, is there a way to call method2 in B when method1 in A be called?
there. I got 2 class
@implemention A
-(void) method1{
something;
}
@end
@implemention B
-(void) method2{
something
}
@end
if the A class' implement can't be modified, is there a way to call method2 in B when method1 in A be called?
It sounds like you are trying to something wierd, but without further information it's hard to give you alternate suggestions.
However. One way to do what that doesn't require changing the implementation of class A is to subclass class A and override the method implementation:
- method1 {
[super method1]; // forward the call on to class A
// Now you can do what ever you want
// Post a notification or call method1 on an instance of class B
}
What you want to do resembles method swizzling . Handle with care. You will find plenty to read around here (objective-c and ios forums), but still and again start here about "handle with care"
In viewDidLoad()
of class B
add an observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method2) name:@"method1Executed" object:nil];
and in method1()
of class A
add the following code
[[NSNotificationCenter defaultCenter] postNotificationName:@"method1Executed" object:nil];
try this
you can create class object in Method1
-(void) method1
{
B *b = [[B alloc] init ]; // Create object of class B
b.method2 // call method of class B
}