Can i know in viewWillAppear that it was called after navigationController pop (back button)?
Asked Answered
A

3

27

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.

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.

Aspiration answered 8/1, 2014 at 20:30 Comment(1)
A
33

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.

Announcement answered 8/1, 2014 at 22:11 Comment(5)
I have tried. It doesn't fit. It looks that there is no solution without writing something in B view controller..Aspiration
Yes, this worked for me!! if (self.isMovingToParentViewController) { // Do something the first time only, not on back button }Houseclean
This answer is incorrect. If the view controller is beneath the one being popped, its isMovingToParentViewController is always false when called from viewWillAppear.Aecium
Helps me a lot.Neile
@Aecium That's what the answer is saying.Calefacient
O
13

I like to do the following in view controller 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.

Ordinate answered 25/11, 2016 at 9:30 Comment(1)
This will result in unexpected behavior if view controller A gets popped, retained, and push back onto the stack.Calefacient
E
3

Add a BOOL property to UIViewController A:

@property (nonatomic) BOOL alreadyAppeared;

Then in your viewWillAppear: method, add:

if (!self.alreadyAppeared) {
    self.alreadyAppeared = YES;
    // Do here the stuff you wanted to do on first appear
}
Expiration answered 8/1, 2014 at 20:36 Comment(3)
But A view controller can be appeared by tab bar click also for example (it is alreadyAppeared but not called by back button)Aspiration
@Aspiration I used delegate for this case in my app.Broccoli
This seems to be the best option for a modal view controller dismissal (I don't want viewWillAppear to be called). Adding this bool fixed it.Spondee

© 2022 - 2024 — McMap. All rights reserved.