applicationWillResignActive解除键盘iPhone

时间:2011-11-04 02:20:36

标签: objective-c ios uiapplicationdelegate

在使用我的应用程序时收到短信,我希望任何打开的键盘都被解雇。如何在app app delegate中的applicationWillResignActive中执行此操作?

2 个答案:

答案 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!")
}

有关更多信息,请关注https://www.hackingwithswift.com/example-code/system/how-to-detect-when-your-app-moves-to-the-background