2014-06-18 89 views

回答

3

在我的工作我的應用程序需要處理這些事件,以及並沒有因此使用下面的兩個通知:

UIApplicationWillResignActiveNotification >>該通知被觸發時,通知中心和控制中心帶大。

UIApplicationWillEnterForegroundNotification >>當通知中心或控制中心被解僱時觸發此通知。

這些事件自然可以從AppDelegate中處理:

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    // Handle notification 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Handle Notification 
} 

雖然這取決於你的應用程序它可能會更容易明確監聽在視圖控制器(一個或多個)要注意的是需要對這些事件這樣的:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; 

...並實現您的選擇:

- (void)appWillResignActive:(NSNotification *)notification 
{ 
    // Handle notification 
} 

- (void)appDidBecomeActive:(NSNotification *)notification 
{ 
    // Handle notification 
} 

當您完成後最後以觀察者身份移除自己:

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // Remove notifications 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; 

    [super viewWillDisappear:animated]; 
}