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.)?
Asked
Active
Viewed 76 times
0
1 Answers
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
- Create a custom class and have all of your ViewControllers inherit from your custom class
- Method Swizzling (similar example here)
Subclassing is probably the best, but if that is impractical for any reason I would look into method swizzling.
-
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