objective-c – 子类中的NSNotificationCenter addObserver

objective-c – 子类中的NSNotificationCenter addObserver,第1张

概述我注册在超类(UIViewController)中被通知,如下所示: SuperClass.m - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self 我注册在超类(UIVIEwController)中被通知,如下所示:

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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/999090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存