我能在viewWillAppear中知道它在navigationController pop(后退按钮)之后被调用吗?

时间:2023-01-27 15:56:19

Say I have UIViewController A and B. User navigates from A to B with a push segue. Than user presses back button and comes to A.

比如我有UIViewController A和B,用户通过push segue从A导航到B。用户按下后退按钮,来到A。

Now viewWillAppear of A is called. Can I know in the code here that I came from back button (navigationController popTo...) and not by another way? And without writing special code in the B view controller.

现在viewWillAppear的A被调用。我可以在这里的代码中知道我是从后退按钮(navigationController popTo…)而不是从另一种方式来的吗?不需要在B视图控制器中编写特殊代码。

3 个解决方案

#1


25  

hm, maybe you can use self.isMovingToParentViewController in viewWillAppear, see docs, if it is NO then it means the current view controller is already on the navigation stack.

嗯,也许你可以用self。viewWillAppear中的isMovingToParentViewController,请参见docs,如果不是,则表示当前视图控制器已经在导航堆栈上。

#2


6  

I like to do the following in view controller A:

我想在视图控制器A中做以下事情:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (_popping) {
        _popping = false;
        NSLog(@"BECAUSE OF POPPING");
    } else {
        NSLog(@"APPEARING ANOTHER WAY");
    }

    //keep stack size updated
    _stackSize = self.navigationController.viewControllers.count;

    ....
}
- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    _popping = self.navigationController.viewControllers.count > _stackSize;

    ....
}

What you are doing is keeping track of whether your view controller (A) is disappearing because a view controller (B) is being pushed or for another reason. Then (if you did not modify the child view controller order) it should accurately tell you if (A) is appearing because of a pop on the navigation controller.

你要做的是跟踪你的视图控制器(A)是否因为视图控制器(B)被推或者因为其他原因而消失。然后(如果您没有修改子视图控制器的顺序)它应该准确地告诉您(A)是否因为导航控制器上的弹出而出现。

#3


3  

Add a BOOL property to UIViewController A:

向UIViewController a添加BOOL属性:

@property (nonatomic) BOOL alreadyAppeared;

Then in your viewWillAppear: method, add:

然后在你的viewWillAppear:方法中,添加:

if (!self.alreadyAppeared) {
    self.alreadyAppeared = YES;
    // Do here the stuff you wanted to do on first appear
}

#1


25  

hm, maybe you can use self.isMovingToParentViewController in viewWillAppear, see docs, if it is NO then it means the current view controller is already on the navigation stack.

嗯,也许你可以用self。viewWillAppear中的isMovingToParentViewController,请参见docs,如果不是,则表示当前视图控制器已经在导航堆栈上。

#2


6  

I like to do the following in view controller A:

我想在视图控制器A中做以下事情:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (_popping) {
        _popping = false;
        NSLog(@"BECAUSE OF POPPING");
    } else {
        NSLog(@"APPEARING ANOTHER WAY");
    }

    //keep stack size updated
    _stackSize = self.navigationController.viewControllers.count;

    ....
}
- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    _popping = self.navigationController.viewControllers.count > _stackSize;

    ....
}

What you are doing is keeping track of whether your view controller (A) is disappearing because a view controller (B) is being pushed or for another reason. Then (if you did not modify the child view controller order) it should accurately tell you if (A) is appearing because of a pop on the navigation controller.

你要做的是跟踪你的视图控制器(A)是否因为视图控制器(B)被推或者因为其他原因而消失。然后(如果您没有修改子视图控制器的顺序)它应该准确地告诉您(A)是否因为导航控制器上的弹出而出现。

#3


3  

Add a BOOL property to UIViewController A:

向UIViewController a添加BOOL属性:

@property (nonatomic) BOOL alreadyAppeared;

Then in your viewWillAppear: method, add:

然后在你的viewWillAppear:方法中,添加:

if (!self.alreadyAppeared) {
    self.alreadyAppeared = YES;
    // Do here the stuff you wanted to do on first appear
}