1

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?

Mark.zh
  • 15
  • 4
  • 1
    What do you mean by can't be modified ? – Geekoder Oct 14 '13 at 08:55
  • I'm not entirely sure what you mean, but a delegate/protocol seems to be the way to go with this. – Thomas Denney Oct 14 '13 at 08:56
  • i mean i can't insert any new code in implemention A. – Mark.zh Oct 14 '13 at 08:57
  • yes , i known delegate and protocol, what i want is there a way that i can add something like observer on an Object's method execution. – Mark.zh Oct 14 '13 at 08:59
  • My gut reaction was, "Use inheritance," but it's impossible to say whether that's the correct solution without knowing the motivation behind it. – Marcelo Cantos Oct 14 '13 at 09:01
  • check out the [CJAInvocation](https://github.com/carlj/CJAInvocation) library. wit this library you can set a before and after filter for an custom method – CarlJ Oct 14 '13 at 09:01
  • certainly, i'm using cocos2d-iphone, we got scene that need load user's big photo and other details. We fetch the photo with Async http. But when http loading, the user may close the window, so that, when finish downloading, the reference for the photo's layer has been an release object, app will crash. – Mark.zh Oct 14 '13 at 09:09
  • So, if i can get a callback or notify when layer's onExit executed, that callback can make the http's reference to be nil. – Mark.zh Oct 14 '13 at 09:13
  • A practical solution would be to use method swizzling. I won't give much detail here, because ahead are monsters. – Cyrille Oct 14 '13 at 09:16
  • Yeah, I've find a exchange_implement() method in runtime reference. – Mark.zh Oct 14 '13 at 09:19

4 Answers4

1

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
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • I was writing the same thing, I think this is the only thing to do if you can't touch class A. – KIDdAe Oct 14 '13 at 09:08
1

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"

Community
  • 1
  • 1
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
0

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];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
-1

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
}
Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
Indra
  • 528
  • 2
  • 11