NSNotificationCenter:它是否可以监听其他未明确发布通知的类?

发布于 2024-11-11 17:44:17 字数 267 浏览 6 评论 0原文

我正在尝试监听被触发的 UIViewController 方法(-viewDidLoad-viewDidAppear-viewDidUnload-viewDidDisappear 等),无需修改方法中的实际代码。这不可能吗?我试图让另一个类中的观察者监听正在调用的这些方法,但我不想将 postNotifications: 添加到视图控制器。我必须这样做吗?

I am trying to listen for UIViewController methods being triggered (-viewDidLoad, -viewDidAppear, -viewDidUnload, -viewDidDisappear and so on) without modifying the actual code in the methods. Is this impossible? I'm trying to have an observer in another class listen in for those methods being called, but I don't want to add postNotifications: to the View Controller. Do I have to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-11-18 17:44:17

不,这是不可能的。恐怕您必须添加 postNotifications:

No, it is not possible. You will have to add postNotifications: I'm afraid.

枕梦 2024-11-18 17:44:17

我将 UINavigationController 子类化以发布您想要的通知。或者,我可以使用类别的 swizzling 来添加通知,而无需触及类的代码

I'd subclass the UINavigationController to post the notifications you want. Alternatively I'd use swizzling with a category to add the notifications without touching the code of the class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文