我有父母的UIViewController A有一些屬性。我有孩子UIViewController B,C,D這是繼承自A.檢測父類變量或屬性是由其他子類更改objective-c
我的問題是: 在乙,我改變父屬性值。 C,D如何知道父值從B改變了?
基本上我想分享孩子之間的屬性。
在此先感謝。
代碼示例;
@interface A : UIViewController{
NSString *string1; //This is just example
}
@property (nonatomic, strong) NSString *string2;
@end
Child B級
@interface B : A
@end
@implementation B
//here I change string1 and string2;
@end
Child C級
@interface C : A
@end
@implementation C
//here I want to get changed string1 and string2 by Child A;
@end
兒童d
@interface D : A
@end
@implementation D
//here I want to get changed string1 and string2 by Child A;
@end
目前,我使用辛格爾頓來存儲所有的VA魯和它運作良好,但我認爲應該有更好的辦法在這種情況下?
父類/子類的層次結構還是視圖層次? – trojanfoe 2013-03-05 16:22:15
KVO或模式觀察者。你可以谷歌它兩個。取決於你如何處理依賴關係,以及何時需要綁定它們。 – 2013-03-05 16:23:47
'Singleton'和'NSNotification'將是一個很好的解決方案。當你改變單身人士的價值發送一個通知給C和D,並運行/調用C和D的方法。對我來說 – 2013-03-05 16:23:51