0

What's the best way to execute a given block globally at certain phases of a UIViewController, in particular, before a view controller is hidden from view (dismissed, another view controller pushed, etc.)?

jszumski
  • 7,430
  • 11
  • 40
  • 53
nmock
  • 1,907
  • 2
  • 20
  • 29

1 Answers1

0

Just override the builtin notifications:

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

  // Do what you need to do

}

Also see the other notifications:

– viewWillAppear:
– viewDidAppear:
– viewWillDisappear:
– viewDidDisappear:
– viewWillLayoutSubviews
– viewDidLayoutSubviews

Update: If you need to do this for all UIViewControllers in your app, you have a these choices

  1. Create a custom class and have all of your ViewControllers inherit from your custom class
  2. Method Swizzling (similar example here)

Subclassing is probably the best, but if that is impractical for any reason I would look into method swizzling.

Community
  • 1
  • 1
Skotch
  • 3,072
  • 2
  • 23
  • 43
  • I'm aware of this, but, see my *global* specification. This solution would require me to go through each subclass (and all future subclasses) and add the code that I'd need to happen. (Also, not easily modifiable) – nmock May 03 '13 at 00:35