在使用我的应用程序时收到短信,我希望任何打开的键盘都被解雇。如何在app app delegate中的applicationWillResignActive中执行此操作?
答案 0 :(得分:4)
像answer中的示例一样实现代码。让您的视图控制器注册UIApplicationWillResignActiveNotification
。当通知触发时,请致电resignFirstResponder
。这样就可以避免UIApplicationDelegate
和视图控制器之间的紧密耦合。假设您的视图控制器有一个名为UITextField
的{{1}}:
textField
答案 1 :(得分:0)
对于Swift 5实现,请尝试
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)
}
@objc func appMovedToBackground() {
print("App moved to background!")
}