如何在Swift 4.2中编写键盘通知?

时间:2019-02-16 18:03:48

标签: ios swift swift3 nsnotificationcenter swift4.2

我正在尝试将此代码从Swift 3更新为Swift 4.2

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: .UIKeyboardDidShow, object: nil);

到目前为止,我刚刚尝试了编译器给出的自动更正。结果是这样的代码:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: .UIResponder.keyboardDidShowNotification, object: nil);

不幸的是,这并没有使我走得太远,从而导致了其他错误: “表达类型不明确,没有更多上下文”
有人解决了吗? enter image description here

1 个答案:

答案 0 :(得分:4)

只需将.UIResponder.keyboardDidShowNotification替换为UIResponder.keyboardDidShowNotification,即可解决您的问题。

最终代码将是:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)