2013-10-14 29 views
1

時發送通知。我有2類是否有任何解決方案,當對象在那裏執行ios

@implemention A 

-(void) method1{ 
something; 
} 
@end 

@implemention B 

-(void) method2{ 
something 
} 
@end 

如果A類的實現不能被修改,有沒有辦法來調用方法2.在B時的方法1在一個被稱爲?

+1

你是什麼意思不能被修改? – Geekoder

+0

我並不完全確定你的意思,但代表/協議似乎是解決這個問題的方法。 –

+0

我的意思是我不能在實現中插入任何新的代碼A. –

回答

-1

試試這個

您可以在方法一

-(void) method1 
{ 
    B *b = [[B alloc] init ]; // Create object of class B 
    b.method2 // call method of class B 
} 
+0

OP無法修改A. –

+0

的執行是的,無法修改。我嘗試在ios運行時函數中找到某種方法。 –

+0

比嘗試重寫第一類的方法1。 – Indra

1

這聽起來像你想什麼奇怪的,但沒有進一步的信息很難給你備用建議創建類的對象。

但是,做什麼,不需要改變A類實現的一種方法是子類A級並重寫方法實現:

- 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 
} 
+0

我正在寫同樣的東西,我認爲如果你不能觸及A類,這是唯一要做的事情。 – KIDdAe

0

class BviewDidLoad()添加一個觀察者

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

並在method1()class A添加以下代碼

[[NSNotificationCenter defaultCenter] postNotificationName:@"method1Executed" object:nil]; 
相關問題