如何从导航堆栈中推送/弹出uiviewcontroller时收到警报
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从导航堆栈中推送/弹出uiviewcontroller时收到警报相关的知识,希望对你有一定的参考价值。
当我的视图控制器被从导航堆栈中推出或弹出时,我需要做某些事情,但是不想使用viewillappear / viewdidappear或viewwilldisappear / viewdiddisappear,因为除了推送/弹出视图控制器之外,还包括这些情况。是正确的方法去使用navigationcontroller委托和navigationController:didShowViewController:animated:和navigationController:willShowViewController:animated:?如果没有,最好的办法是什么?
要知道什么时候推,你可以使用
UINavigationControllerDelegate
并实施
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
只要将viewcontroller推入导航堆栈,并且每当弹出顶部的viewcontroller时,此方法就会触发,从而再次显示它。因此,您必须使用一个标志来确定它是否已经初始化,如果它没有意味着它只是被推动了。
要了解它何时被弹出,请使用以下答案:
您可以尝试在对象推送或从导航控制器堆栈弹出时调用的UINavigationController委托方法。
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
这是一个示例,通过重写-viewWillAppear:
并通过覆盖-viewWillDisappear:
来检测何时将视图控制器推入导航堆栈
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.isMovingToParentViewController) {
NSLog(@"view controller being pushed");
}
}
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.isMovingFromParentViewController) {
NSLog(@"view controller being popped");
}
}
您始终可以创建UINavigationController的简单子类并包装其超类的方法,以便在调用它们之前设置一个标志:
Action NavigationController.h
#import <UIKit/UIKit.h>
@interface ActionNavigationController : UINavigationController
@property (nonatomic, readonly) BOOL pushing;
@end
行动NavigationController.m
#import "ActionNavigationController.h"
@implementation ActionNavigationController
@synthesize pushing = _pushing;
-(void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated {
_pushing = YES;
[super pushViewController:viewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
_pushing = NO;
return [super popViewControllerAnimated:animated];
}
- (NSArray *)popToViewController:(UIViewController *)viewController
animated:(BOOL)animated {
_pushing = NO;
return [super popToViewController:viewController animated:animated];
}
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
_pushing = NO;
return [super popToRootViewControllerAnimated:animated];
}
@end
由于pushing
将评估NO
事件,如果没有发生任何事情,预计将从UINavigationControllerDelegate访问此代码。
小心使用
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
如果用户从边缘向右滑动以弹出视图控制器(而不是实际弹出它),它将调用上面的委托函数而不是下面的函数
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated;
请参考https://gist.github.com/nonamelive/9334458
你可以在willShowViewController中做类似的事情
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
if self.navigationController!.viewControllers.contains(self){
print ("push")
} else {
print ("pop")
}
}
以上是关于如何从导航堆栈中推送/弹出uiviewcontroller时收到警报的主要内容,如果未能解决你的问题,请参考以下文章