我想在 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/