亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢測應用程序何時進入我的視圖背景的最佳方法是什么?

檢測應用程序何時進入我的視圖背景的最佳方法是什么?

iOS
叮當貓咪 2019-12-11 14:09:01
我有一個視圖控制器,它使用NSTimer來執行一些代碼。檢測應用何時進入后臺以便暫停計時器的最佳方法是什么?
查看完整描述

3 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

當應用程序進入后臺時,您可以對任何感興趣的類進行接收通知。這是將這些類與AppDelegate耦合的不錯選擇。


在初始化所述類時:


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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

回應通知


-(void)appWillResignActive:(NSNotification*)note

{


}

-(void)appWillTerminate:(NSNotification*)note

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];


}



查看完整回答
反對 回復 2019-12-12
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

對于那些希望在Swift中做到這一點的人:


開init:


NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplicationWillResignActiveNotification, object: nil)

開deinit:


NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillResignActiveNotification, object: nil)

響應通知:


dynamic private func applicationWillResignActive() {

    // Do things here

}

Apple鼓勵我們在Swift中盡可能避免使用動態調度和Objective-C選擇器,但這仍然是最方便的方法。



查看完整回答
反對 回復 2019-12-12
  • 3 回答
  • 0 關注
  • 469 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號