我在viewdidload()
NotificationCenter.default.addObserver(self, selector: Selector(("appWillEnterForeground:")), name: NSNotification.Name.UIApplicationDidBecomeActive, object: UIApplication.shared)
调用此函数
func appWillEnterForeground(notification: NSNotification!) {
// update label text here
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
NotificationCenter.default.removeObserver(self)
}
但该应用程序在返回前台时崩溃
unrecognized selector sent to instance 0x160db800
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ appWillEnterForeground:]: unrecognized selector sent to instance 0x160db800'
答案 0 :(得分:0)
appWillEnterForeground:
告诉通知中心查找具有该名称的方法,但该方法需要一个参数。您对该方法的实现不带参数,因此不被视为正确的匹配。
尝试Selector(("appWillEnterForeground")
。