3 回答

TA貢獻1946條經(jīng)驗 獲得超3個贊
應(yīng)用程序中的任何類都可以成為應(yīng)用程序中不同通知的“觀察者”。創(chuàng)建(或加載)視圖控制器時,您需要將其注冊為觀察者,UIApplicationDidBecomeActiveNotification并指定在將通知發(fā)送到應(yīng)用程序時要調(diào)用的方法。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
不要忘記自己清理!當(dāng)您的視圖消失時,請記住將自己移除為觀察者:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
有關(guān)通知中心的更多信息。

TA貢獻1895條經(jīng)驗 獲得超7個贊
Swift 3,4等效:
添加觀察者
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
刪除觀察者
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
打回來
@objc func applicationDidBecomeActive() {
// handle event
}

TA貢獻1856條經(jīng)驗 獲得超17個贊
Swift 2等效:
let notificationCenter = NSNotificationCenter.defaultCenter()
// Add observer:
notificationCenter.addObserver(self,
selector:Selector("applicationWillResignActiveNotification"),
name:UIApplicationWillResignActiveNotification,
object:nil)
// Remove observer:
notificationCenter.removeObserver(self,
name:UIApplicationWillResignActiveNotification,
object:nil)
// Remove all observer for all notifications:
notificationCenter.removeObserver(self)
// Callback:
func applicationWillResignActiveNotification() {
// Handle application will resign notification event.
}
- 3 回答
- 0 關(guān)注
- 1177 瀏覽
添加回答
舉報