1

那里。我有 2 节课

@implemention A

-(void) method1{
something;
}
@end

@implemention B

-(void) method2{
 something
}
@end

如果A类的实现不能修改,有没有办法在调用A中的method1时调用B中的method2?

4

4 回答 4

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
}
于 2013-10-14T09:06:05.327 回答
1

你想做的类似于方法调配。小心轻放。您会在此处找到大量可阅读的内容(objective-c 和 ios 论坛),但仍然一次又一次地从此处开始“小心处理”

于 2013-10-14T12:19:39.093 回答
0

添加viewDidLoad()观察者_class B

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method2) name:@"method1Executed" object:nil];

并添加method1()以下class A代码

[[NSNotificationCenter defaultCenter] postNotificationName:@"method1Executed" object:nil];
于 2013-10-14T11:29:24.050 回答
-1

尝试这个

您可以在 Method1 中创建类对象

-(void) method1
{
     B *b = [[B alloc] init ]; // Create object of class B 
     b.method2   // call method of class B
}
于 2013-10-14T08:59:58.133 回答