我在父被通知註冊(UIViewController的),像這樣:NSNotificationCenter的addObserver在子類中
SuperClass.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notification:)
name:@"Notification"
object:nil];
}
- (void)notification:(NSNotification *)notification {
// Do something for SuperClass with the notification
}
現在在子類(SuperClass.m的子類),我也聽相同的通知,像這樣:
SubClass.m
- (void)notification:(NSNotification *)notification {
// Do something specific for SubClass with the notification
}
這是一種可接受的(代碼)方式來處理在超類中的通知上進行操作時的一般行爲,以及在對子類中的通知進行操作時具有更具體的行爲?
是的。評論必須太長。 – jrturton
是的,當你調用這個通知函數時使用它,它在超級類及其子類中被調用。你可以認爲這是可以接受的代碼,如果你想在所有視圖中加載數據,例如當你的應用程序來自後臺時你需要刷新所有的數據.. –
好吧。起初編碼時,我認爲只會調用子類的方法,但由於某種原因,似乎調用了超類「觀察者和子類」的觀察者。這就是爲什麼我沒有在我的子類的觀察者中調用超級。 –