In ios how to add an observer to a method?

183 Views Asked by At

I'm using a plugin,there is a method "A" in the plugin's pluginClass. if "A" is called ,I want to call "B" in myCalss.Such as:

-(void)A{
  [myClass B];
}

But I don't want to modify the code in pluginClass. Is there any way to add an observer to a method or similar? Thank u.

2

There are 2 best solutions below

2
David Berry On

The only acceptable option I can think of is to subclass the class from the plugin, and then within your subclass over the target method and forward it:

@implementation MyPluginClass : PluginClass

-(void) A {
    [super A];
    [myClass B];
}

@end
1
GeneCode On

Seems really easy solution. Make your own method C that contains A function and add method B in C. Instead of calling A, then you call C.

// BEFORE

[pluginClass A]; // a call to method A that u use originally.

// AFTER

[self C];

-(void)C {
  [pluginClass A];
  [myClass B];
}