# iOS在APP进入后台后通知viewController，调用其方法。

**URL:** <https://www.shiqidu.com/t/topic/477>\
**Category:** 开发调优\
**Tags:** C-C-Obj-C\
**Created:** [2018 年8 月 23 日 16:18 UTC](https://www.shiqidu.com/t/topic/477 "2018-08-23T16:18:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2018 年8 月 23 日 16:18 UTC](https://www.shiqidu.com/t/topic/477/1 "2018-08-23T16:18:05Z")

</div>

使用 `NSNotificationCenter`通知中心来完成此功能。

APP在进入后台时会调用`AppDelegate` 的 `applicationDidEnterBackground`方法。

所以只要在此方法中通知viewController即可。

所以首先在viewController中的`viewDidLoad`中添加一个观察者

```C
- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterBackgroundNotification) name:@"enterBackground" object:nil];
}

```

然后在 `AppDelegate` 的 `applicationDidEnterBackground`方法中通知这个观察者。

```C
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"enterBackground" object:nil];
}

```
