iOS 在viewController中監聽Home鍵觸發以及重新進入界面的方法


 

第一步:
創建2個NSNotificationCenter監聽
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil]; //監聽是否觸發home鍵掛起程序.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil]; //監聽是否重新進入程序程序.

 


第二步:
實現2個NSNotificationCenter所觸發的事件方法
- (void)applicationWillResignActive:(NSNotification *)notification

{
printf("按理說是觸發home按下\n");
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
printf("按理說是重新進來后響應\n");
}

 



注: 在home鍵觸發后,AppDelegate響應的方法為:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers,

and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM