当导航控制器推/拉 UIViewController 时,如何添加和删除通知观察者?

发布于 2024-12-03 00:48:05 字数 245 浏览 1 评论 0原文

我通常在 init 方法中添加 UINotification 观察者,然后在 dealloc 中删除它们。

但是,如果我有一个由 UINavigationController 推送的 UIViewController 链,则在推送下一个 UIViewController 时它们不会被释放。结果他们都在观察通知,这不是我想要的。

当导航控制器推送/拉动 UIViewController 时,如何添加和删除通知观察者?

I usually add the UINotification observer in the init method and I remove them in dealloc.

However if I have a chain of UIViewControllers pushed by a UINavigationController, they are not deallocated when the next UIViewController is pushed. Consequently they all observe for the notification, and this is not what I want.

How can I add and remove notification observers when a UIViewController is pushed/pulled by a navigation controller ?

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

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

发布评论

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

评论(3

请恋爱 2024-12-10 00:48:05

为了获得通知,您可以设置 UINavigationController 的委托。但这相当麻烦,因为导航控制器只有一个委托。所以在这种情况下,我会使用 viewDidAppear:animatedviewDidDisappear:animated 等等。当导航控制器隐藏和显示它们时,这些方法将在您的视图控制器上调用,如果您呈现模式视图控制器,这些方法也会被调用,在这种情况下您可能还想取消注册通知。

In order to get notified, you can set the delegate of the UINavigationController. This is quite cumbersome though, since the navigation controller only has one delegate. So in this case, I would have used viewDidAppear:animated, viewDidDisappear:animated and so on. These methods will be called on your view controllers as the navigation controller hides and shows them, and will also be called if you present a modal view controller in which case you probably also want to unregister notifications.

二智少女 2024-12-10 00:48:05

添加第二个答案,其中包含有关如何使用 UINavigationControllerDelegate 实现此目的的示例。

在某处,将委托设置为根视图控制器。使用代码或将其连接到笔尖中。让您的根视图控制器成为 UINavigationControllerDelegate。

@interface MyViewController : UIViewController <UINavigationControllerDelegate>
    // ...
@end

在根视图控制器的实现中执行此操作

@implementation MyViewController
- (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated
{
    [viewController
        performSelector:@selector(willBeShownViaNavigationController)];

    [navigationController.visibleViewController 
        performSelector:@selector(willBeHiddenViaNavigationController)];
}
@end

确保导航控制器中使用的所有视图控制器都实现这两个方法。

注意:此代码未经测试,可能存在一些错误。但你应该明白了。

Adding a second answer with an example on how to achieve this with a UINavigationControllerDelegate.

Somewhere, set the delegate to the root view controller. Either with code or by connecting it in a nib. Make your root view controller a UINavigationControllerDelegate.

@interface MyViewController : UIViewController <UINavigationControllerDelegate>
    // ...
@end

Do this in the implementation of the root view controller

@implementation MyViewController
- (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated
{
    [viewController
        performSelector:@selector(willBeShownViaNavigationController)];

    [navigationController.visibleViewController 
        performSelector:@selector(willBeHiddenViaNavigationController)];
}
@end

Make sure all the view controllers being used in that navigation controller implements those two methods.

Note: this code is untested, there may be some errors. But you should get the idea.

送你一个梦 2024-12-10 00:48:05

您需要子类化 UINavigationController 来跟踪它是推送还是弹出。然后在 viewWillAppear 中,您可以检查是否被推送或弹出。我有一个子类这里

You need to subclass UINavigationController to keep track of whether it is pushing or popping. Then in your viewWillAppear you can check to see if you're being pushed or popped. I have a subclass for that Here

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