1. 程式人生 > >iOS—在controller裡面自定義方法判斷:home鍵進入後臺,返回前臺

iOS—在controller裡面自定義方法判斷:home鍵進入後臺,返回前臺

1、定義進入前臺時呼叫的函式: 
- (void)applicationWillEnterForeground:(NSNotification *)notification 

//進入前臺時呼叫此函式 
我們可以在這個方法裡寫上我們需要重新整理的程式碼 

2、註冊呼叫上面函式的通知,在willAppear中: 
- (void)viewWillAppear:(BOOL)animated 

UIApplication *app = [UIApplication sharedApplication]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(applicationWillEnterForeground:) 
name:UIApplicationWillEnterForegroundNotification 
object:app]; 
}

3、反註冊通知,在willDisappear中: 
- (void)viewDidDisappear:(BOOL)animated 

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
}