开发者

Objective C - How to detect applicationWillResignActive from other ViewController

开发者 https://www.devze.com 2023-02-21 20:36 出处:网络
I\'m writing an app that uses many ViewControllers. Is there anyway I can detect applicationWillResignActive on these ViewControllers rather than passing that event from the AppDelegate class? (i.e [s

I'm writing an app that uses many ViewControllers.

Is there anyway I can detect applicationWillResignActive on these ViewControllers rather than passing that event from the AppDelegate class? (i.e [self.viewController1 suspend] etc)

Some of the ViewControllers might not have been instantiated yet and I'd have to do too many if else from the default AppDelegate handler to pass the event to every single possible ViewControllers.

I tried adding the below code snippets but doesn't seem to do the trick.

In t开发者_如何学JAVAhe ViewController's viewDidLoad method

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(foo) name:@"AppResigned" object:nil];

In the AppDelegate default applicationWillResignActive method

[[NSNotificationCenter defaultCenter] postNotificationName:@"AppResigned" object:nil];

Thanks,

Tee


When I moved the addObserver to init it works. Weirdness.


Works for me even if the addObserver call is made in viewDidLoad. Using iOS 6 SDK.

0

精彩评论

暂无评论...
验证码 换一张
取 消