Checking if a UIViewController is about to get Popped from a navigation stack?

后端 未结 15 987
说谎
说谎 2020-12-04 19:17

I need to know when my view controller is about to get popped from a nav stack so I can perform an action.

I can\'t use -viewWillDisappear, because that gets called

15条回答
  •  一个人的身影
    2020-12-04 19:30

    I tried this:

    - (void) viewWillDisappear:(BOOL)animated {
        // If we are disappearing because we were removed from navigation stack
        if (self.navigationController == nil) {
            // YOUR CODE HERE
        }
    
        [super viewWillDisappear:animated];
    }
    

    The idea is that at popping, the view controller's navigationController is set to nil. So if the view was to disappear, and it longer has a navigationController, I concluded it was popped. (might not work in other scenarios).

    Can't vouch that viewWillDisappear will be called upon popping, as it is not mentioned in the docs. I tried it when the view was top view, and below top view - and it worked in both.

    Good luck, Oded.

提交回复
热议问题