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

问题描述 投票:1回答:1

我正在尝试将此代码从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

ios swift swift3 nsnotificationcenter swift4.2
1个回答
4
投票

只需用.UIResponder.keyboardDidShowNotification替换UIResponder.keyboardDidShowNotification,它就能解决你的问题。

最终的代码将是:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
© www.soinside.com 2019 - 2024. All rights reserved.