Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle UIApplicationDelegate methods in an UIViewController?

I have a UIViewController on a tab of a tab based app, that hooks up UIApplicationDelegate. I'd like to handle app events by UIApplicationDelegate, but I don't get them in my UIViewController, its methods are not called.

What should I do beside hook up it in my UIViewController's interface declaration?

@interface TestViewController : UIViewController<UIApplicationDelegate>
@end

Other delegates are works, I can handle them, except this, UIApplication and its delegate should have some trivial 'trick', but I could not find.

Thanks!

like image 762
Tom Avatar asked Jan 15 '23 13:01

Tom


1 Answers

If you need a class other than the app delegate to respond to the various application events, then have the other class register for the various application notifications.

For example, to deal with the app entering the background:

// Put this in the class that needs to deal with entering the background
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];

See the docs for UIApplication for a list of all of the notifications.

Don't forget to remove the observer in the dealloc method of the class.

like image 119
rmaddy Avatar answered Jan 22 '23 15:01

rmaddy