アプリがバックグラウンドに移行したり、フォアグラウンドになった場合はviewWillAppearなどのメソッドは呼ばれません。その場合は、下記のようなNSNotificationCenterを利用して、任意のメソッドを実行させるようにします。
Notification一覧
使いそうなやつだけ記載。
| 種類 | 説明 |
|---|---|
| UIApplicationWillEnterForegroundNotification | フォアグラウンドになる直前に呼ばれる |
| UIApplicationDidBecomeActiveNotification | アクティブになったら呼ばれる |
| UIApplicationWillResignActiveNotification | アクティブでなくなる直前に呼ばれる |
| UIApplicationDidEnterBackgroundNotification | バックグラウンドになったら呼ばれる |
| UIApplicationWillTerminateNotification | 終了する直前に呼ばれる |
実装
各viewControllerのviewDidLoad内で実行したいメソッドをNotificationCenterに登録します
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testMethod) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)testMethod {
NSLog(@"%@", @"test");
}