How to detect if view controller is being popped o

2020-02-17 05:16发布

I currently need to implement some code when the top view controller is being popped off from my navigation controller. Is there a way to detect when the view controller is being popped off the navigation controller stack?

As much as possible I want to stay away from using viewWillDisappear or viewDidDisappear because I'm using a splitview in my project, and selecting a different row in the master view will also trigger the viewWillDisappear/viewDidDisappear methods.

5条回答
倾城 Initia
2楼-- · 2020-02-17 05:19

If you don't need to know before the view controller is removed, and just need to know it has been popped, you can also use deinit.

class ViewController: UIViewController {

    deinit {
        // View controller has been popped/dismissed and it's being released
    }
}

This method works well to notify coordinators or other delegates.

查看更多
相关推荐>>
3楼-- · 2020-02-17 05:25

UPDATE 20150430

Based on phatmann's feedback (first comment below), I was curious if something had changed since I answer this question over a year ago. I put together a simple, example app, and have some results that are interesting.

Option 1, example

https://github.com/greymouser/TestNVC

I don't have the ability to easily test pre-8.x, so I'm not sure if something has changed since then. However, the behavior I originally described does still happen. However, thanks to puting together the test app, I did notice an oddity I didn't before.

If I just rely on {will,did}MoveToParentViewController, I noticed a spurious didMoveToParentViewController: call when pushing the first non-rootVC, on the rootVC, with parent != nil (implying it is added, not being removed). I didn't encounter this around the time of my original answer, as I usually have "permanent" rootVC's on my NVC's, and hadn't implemented the callbacks there. See the example app with logging set to LOG_WILL_DID_MTPVC (in ViewController.m). This is an -- edited for space -- snapshot of what I saw:

TestNVC[] -[vc(rootVC) willMoveToParentViewController [entering]
TestNVC[] -[vc(rootVC) didMoveToParentViewController [entering]
TestNVC[] -[vc(1) willMoveToParentViewController [entering]
TestNVC[] -[vc(rootVC) didMoveToParentViewController [entering]  # <-- this is odd
TestNVC[] -[vc(1) didMoveToParentViewController [entering]
...

My original answer suggested using {will,did}MoveToParentViewController alone, as it was a "one stop shop" to handle this behavior. However, now that I've seen the spurious call to the rootVC, I suggest a mix of {will,did}MoveToParentViewController as well as the standard UINavigationControllerDelegate callbacks. For this behavior in the example app, set logging to LOG_WILL_DID_MTPVC_LEAVING_AND_NVC_WILL_DID_SHOW_VC. Now we see the following:

TestNVC[] -[nvcD willShowViewController]: rootVC
TestNVC[] -[nvcD didShowViewController]: rootVC
TestNVC[] -[nvcD willShowViewController]: 1
TestNVC[] -[nvcD didShowViewController]: 1
TestNVC[] -[nvcD willShowViewController]: 2
TestNVC[] -[nvcD didShowViewController]: 2
TestNVC[] -[vc(2) willMoveToParentViewController [leaving]
TestNVC[] -[nvcD willShowViewController]: 1
TestNVC[] -[vc(2) didMoveToParentViewController [leaving]
TestNVC[] -[nvcD didShowViewController]: 1
TestNVC[] -[vc(1) willMoveToParentViewController [leaving]
TestNVC[] -[nvcD willShowViewController]: rootVC
TestNVC[] -[vc(1) didMoveToParentViewController [leaving]
TestNVC[] -[nvcD didShowViewController]: rootVC

... and this makes much more sense now.

Option 2

Another option I didn't explore is using your NVC sublcass, overriding - pushViewController:animated: and - popViewControllerAnimated:, and applying whatever behaviors you want to the VC being pushed, or the VC that was returned from the pop. (Make sure to remember to call super in your overrides if you attempt this.)

Update summary

So, thanks to phatmann for the chance to readdress this. I think my answer is more correct now. However, I'm not so sure that it was ever "fully non-truthy". ;-)

ORIGINAL

If the exact behavior you described is what you are looking for, then override the following on your child view controller:

- (void)willMoveToParentViewController:(UIViewController *)parent;
- (void)didMoveToParentViewController:(UIViewController *)parent;

willMoveToParentViewController: will get called with parent != nil when entering, and parent == nil when leaving. didMoveToParentViewController: will always have parent != nil.

Sometimes, viewDidDisappear may make sense. However, if you're truly looking for push and pop from the parent container view controller, those methods above are what you want.

查看更多
ら.Afraid
4楼-- · 2020-02-17 05:27

My experience with iOS 13 is that the property value of isMovingFromParent is not always consistent. When you have search controller being in active mode (search text field is tapped), back to parent view will have false value for this property.

Here is my way to determine if a view is from parent or not:

class MyBaseViewController: UIViewController {
    private var _isPushedToAnotherView = false
    var isPushedToAnotherView: Bool {
      return _isPushedToAnotherView
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      super.prepare(for: segue, sender: sender)
      ...
      _isPushedToAnotherView = true
    }
    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      ...
      _isPushedToAnotherView = false
    }
    ...
}

class MyExtendedClass: MyBaseViewController {
  ...
  override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    ...
    if !isPushedToAnotherView {
        // clear resources hold by this class
    }
}
查看更多
再贱就再见
5楼-- · 2020-02-17 05:33

You can detect whether a view is being popped using the isMovingFromParentViewController property for a view controller as shown below:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([self isMovingFromParentViewController])
    {
        NSLog(@"View controller was popped");
    }
    else
    {
        NSLog(@"New view controller was pushed");
    }
}

isMovingFromParentViewController

Returns a Boolean value that indicates that the view controller is in the process of being removed from its parent.

查看更多
Bombasti
6楼-- · 2020-02-17 05:43

For Swift Users (Swift 3 - 4.2):

I wanted to detect when the view controller is being popped from the stack, so I wasn't able to use viewWillDisappear or viewDidDisappear callbacks, since these callbacks will be called when the view controller is not visible anymore, and not when it's popped from the stack.

but you can use the navigation controller Delegates UINavigationControllerDelegate by doing the below:

let your controller conform to UINavigationControllerDelegate:

class ViewController : UIViewController {

      override func viewDidLoad() {
          super.viewDidLoad()
          self.navigationController?.delegate = self
      }

}




extension ViewController : UINavigationControllerDelegate {

     override func willMove(toParentViewController parent: UIViewController?) {

     /*You can detect here when the viewcontroller is being popped*/

     }

}

hope this helps, goodluck

查看更多
登录 后发表回答