我注册在超类(UIViewController)中得到通知,如下所示:
SuperClass.m.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的子类)中,我也侦听相同的通知,如下所示:
子类.m
- (void)notification:(NSNotification *)notification {
// Do something specific for SubClass with the notification
}这是处理超类中的通知时的一般行为,以及在子类中处理通知时具有更具体行为的一种可接受(代码方面)的方法吗?
发布于 2012-12-20 03:41:16
通常,当您希望在子类中允许更特定的行为时,同时仍然维护超类中的一般行为时,您可以使用子类调用super。例如,-[UIViewController viewDidAppear:]文档说:
可以重写此方法以执行与显示视图相关的其他任务。如果重写此方法,则必须在实现的某个点调用
super。
因此,您的通知设置很好(尽管将NSNotification对象作为您期望被覆盖的方法的参数有点奇怪),您也希望调用[super notification:notification]来获取超类的行为。
https://stackoverflow.com/questions/13960943
复制相似问题