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}
这是一种可接受的(代码方式)方法,用于处理超类中的通知时的一般行为,并在处理子类中的通知时具有更具体的行为?
解决方法 通常,当您希望在子类中允许更具体的行为时,仍然保持超类中的一般行为,您可以使用子类调用super.例如,– [UIVIEwController vIEwDIDAppear:]文档说:You can overrIDe this method to perform additional tasks associated with presenting the vIEw. If you overrIDe this method,you must call
super
at some point in your implementation.
所以你的通知设置很好(尽管将一个NSNotification对象作为你希望被覆盖的方法的参数有点奇怪) – 但是你要调用[super notification:notification]以获得超类的行为.
总结以上是内存溢出为你收集整理的objective-c – 子类中的NSNotificationCenter addObserver全部内容,希望文章能够帮你解决objective-c – 子类中的NSNotificationCenter addObserver所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)