時發送通知。我有2類是否有任何解決方案,當對象在那裏執行ios
@implemention A
-(void) method1{
something;
}
@end
@implemention B
-(void) method2{
something
}
@end
如果A類的實現不能被修改,有沒有辦法來調用方法2.在B時的方法1在一個被稱爲?
時發送通知。我有2類是否有任何解決方案,當對象在那裏執行ios
@implemention A
-(void) method1{
something;
}
@end
@implemention B
-(void) method2{
something
}
@end
如果A類的實現不能被修改,有沒有辦法來調用方法2.在B時的方法1在一個被稱爲?
你想要做什麼類似於method swizzling。小心輕放。你會發現很多東西在這裏閱讀(objective-c和ios論壇),但仍然從這裏開始約"handle with care"
試試這個
您可以在方法一
-(void) method1
{
B *b = [[B alloc] init ]; // Create object of class B
b.method2 // call method of class B
}
這聽起來像你想什麼奇怪的,但沒有進一步的信息很難給你備用建議創建類的對象。
但是,做什麼,不需要改變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
}
我正在寫同樣的東西,我認爲如果你不能觸及A類,這是唯一要做的事情。 – KIDdAe
在class B
viewDidLoad()
添加一個觀察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method2) name:@"method1Executed" object:nil];
並在method1()
的class A
添加以下代碼
[[NSNotificationCenter defaultCenter] postNotificationName:@"method1Executed" object:nil];
你是什麼意思不能被修改? – Geekoder
我並不完全確定你的意思,但代表/協議似乎是解決這個問題的方法。 –
我的意思是我不能在實現中插入任何新的代碼A. –