ios - 当 View Controller 弹出或插入导航 Controller 堆栈时如何获得通知

标签 ios uinavigationcontroller navigation stack

我想在 View Controller 弹出或插入导航 Controller 堆栈时收到通知。 我试着用

- (void)setViewControllers:(NSArray *)viewControllers

但是我失败了。 而且我不想使用委托(delegate)方法来实现这个...

最佳答案

您可以继承 UINavigationController 并覆盖一些方法:

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"poped" object:nil userInfo:@{}];
    return [super popToRootViewControllerAnimated:animated];
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"poped" object:nil userInfo:@{}];
    return [super popToViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"poped" object:nil userInfo:@{}];
    return [super popViewControllerAnimated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushed" object:nil userInfo:@{}];
    return [super pushViewController:viewController animated:animated];
}

我留下了用户信息,如@{},但如果你愿意,你可以在那里放置一些东西,比如添加或删除的 Controller 。

我不知道,但我认为您应该三思,如果您的架构需要针对这种情况进行通知。

此外,您还必须检查 pop 方法是否相互调用,在这种情况下,您几乎不会收到一次 pop 的通知。

关于ios - 当 View Controller 弹出或插入导航 Controller 堆栈时如何获得通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493977/

相关文章:

ios - 如何使用工厂设计模式使用自定义参数初始化 View Controller

ios - 导航图标在带有 xcode 9 的 ios 11 中不显示?

xamarin.forms - Prism Forms INavigatedAware 和 INavigatingAware 之间的区别?

html - 导航栏内的文本在其他屏幕尺寸上自动移至右侧

ios - 核心数据在枚举时发生了变异

ios - 自定义类对象的 NSMutableArray 序列化和反序列化

ios - 将 UINavigationItem 设置为 nil 后,如何重新添加它?

ios - 如何在iOS 11中复制默认导航栏背景图片?

CSS 导航左侧间隙

android - 压缩 Couchdb 复制流量