Answers:
您可以使用以下内容。
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
// View is disappearing because a new view controller was pushed onto the stack
NSLog(@"New view controller was pushed");
} else if ([viewControllers indexOfObject:self] == NSNotFound) {
// View is disappearing because it was popped from the stack
NSLog(@"View controller was popped");
}
}
当然,这是可能的,因为在调用viewWillDisappear时,UINavigationController的视图控制器堆栈(通过viewControllers属性公开)已经更新。
viewWillAppear
其中,似乎无论是通过推动视图控制器来显示视图控制器还是在弹出视图控制器之上的东西,viewControllers数组在两种方式上都是相同的!有任何想法吗?
viewDidLoad
因为它仅被调用一次!嗯,棘手的一个!
![viewControllers containsObject:self]
代替的原因[viewControllers indexOfObject:self] == NSNotFound
?风格选择?
-isMovingFromParentViewController
下面提到的方法使您可以测试是否显式弹出了视图。
我认为最简单的方法是:
- (void)viewWillDisappear:(BOOL)animated
{
if ([self isMovingFromParentViewController])
{
NSLog(@"View controller was popped");
}
else
{
NSLog(@"New view controller was pushed");
}
[super viewWillDisappear:animated];
}
迅速:
override func viewWillDisappear(animated: Bool)
{
if isMovingFromParent
{
print("View controller was popped")
}
else
{
print("New view controller was pushed")
}
super.viewWillDisappear(animated)
}
从UIViewController.h中的Apple文档中:
“这四种方法可以在视图控制器的外观回调中使用,以确定它是否作为子视图控制器被呈现,取消或添加或删除。例如,视图控制器可以检查它是否由于取消而消失了,或者通过检查表达式([self isBeingDismissed] || [self isMovingFromParentViewController])来弹出自己的viewWillDisappear:方法。
- (BOOL)isBeingPresented NS_AVAILABLE_IOS(5_0);
- (BOOL)isBeingDismissed NS_AVAILABLE_IOS(5_0);
- (BOOL)isMovingToParentViewController NS_AVAILABLE_IOS(5_0);
- (BOOL)isMovingFromParentViewController NS_AVAILABLE_IOS(5_0);
因此,是的,唯一记录在案的方法是以下方法:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self isBeingDismissed] || [self isMovingFromParentViewController]) {
}
}
Swift 3版本:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isBeingDismissed || self.isMovingFromParentViewController {
}
}
我发现苹果对此的文档很难理解。此扩展程序有助于查看每个导航的状态。
extension UIViewController {
public func printTransitionStates() {
print("isBeingPresented=\(isBeingPresented)")
print("isBeingDismissed=\(isBeingDismissed)")
print("isMovingToParentViewController=\(isMovingToParentViewController)")
print("isMovingFromParentViewController=\(isMovingFromParentViewController)")
}
}
这适用于iOS7,不知道是否适用于其他任何iOS7。据我所知,viewDidDisappear
该视图中已经弹出。这意味着当您查询时,self.navigationController.viewControllers
您会得到一个nil
。因此,只需检查是否为零即可。
TL; DR
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (self.navigationController.viewControllers == nil) {
// It has been popped!
NSLog(@"Popped and Gone");
}
}
在iOS 6+中,Segues是解决此问题的非常有效的方法。如果您已在Interface Builder中为特定序列指定了标识符,则可以在中进行检查prepareForSegue
。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"LoginSegue"]) {
NSLog(@"Push");
// Do something specific here, or set a BOOL indicating
// a push has occurred that will be checked later
}
}
感谢@Bryan Henry,仍然可以在Swift 5中工作
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let controllers = navigationController?.children{
if controllers.count > 1, controllers[controllers.count - 2] == self{
// View is disappearing because a new view controller was pushed onto the stack
print("New view controller was pushed")
}
else if controllers.firstIndex(of: self) == nil{
// View is disappearing because it was popped from the stack
print("View controller was popped")
}
}
}
这是完成与sbrocket的回答相同的事情的类别:
标头:
#import <UIKit/UIKit.h>
@interface UIViewController (isBeingPopped)
- (BOOL) isBeingPopped;
@end
资源:
#import "UIViewController+isBeingPopped.h"
@implementation UIViewController (isBeingPopped)
- (BOOL) isBeingPopped {
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
return NO;
} else if ([viewControllers indexOfObject:self] == NSNotFound) {
return YES;
}
return NO;
}
@end