“NSNotification.Name”类型没有“keyboardDidShowNotification”成员。

29

我在使用Swift 4.2时遇到了这个错误:

Type 'NSNotification.Name' has no member 'keyboardDidShowNotification'

这是我的代码:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.keyboardDidShowNotification, object: nil)

以下代码在Swift 4中工作正常,但在Swift 4.2中不起作用。

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

输入图像描述

苹果文档参考:NSNotification.Name.keyboardDidShowNotification


就您的“按钮类型”问题而言...@Krunal - 这可能会有所帮助...(不是我的,我也没有尝试过...只是通过搜索找到的): https://gist.github.com/benasher44/de0a75a4583e5f57853998142e795776 - DonMag
@DonMag - 谢谢您的回复。非常感谢。但是看起来像是Swift4.2的补丁/支持。我正在迁移整个项目。 - Krunal
1
当然可以……但是这份要点可能是这些语法改变类型的一个不错的参考。至于迁移整个项目,Xcode 10 确实有一个迁移助手可以提供帮助。参考:https://swift.org/migration-guide-swift4.2/ - DonMag
6个回答

100

我相信你现在是这样使用它的。

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

/* You can substitute UIResponder with any of it's subclass */

这被列在UIResponder 文档中,作为一个Type Property


1
奇怪的是,.keyboardDidShowNotificationUIWindowUIResponder 中都可以找到。 - rmaddy
@rmaddy,我实际上是从Xcode的建议中得到这个。但是当我转到通知页面的文档时,它显示在“UIWindow”下面。 :/ - Rakesha Shastri
奇怪的是,两个版本(UIWindow 和 UIResponder)都可以编译。 - rmaddy
@rmaddy 我找到了原因,我认为是因为_"许多关键对象也是响应者,包括UIApplication对象、UIViewController对象和所有UIView对象(其中包括UIWindow)"_ https://developer.apple.com/documentation/uikit/uiresponder - Rakesha Shastri
.keyboardDidShowNotification 必须在 UIResponder 中声明,因为我可以使用 UIView.keyboardDidShowNotification 或任何最终从 UIResponder 继承的类。 - rmaddy
我真的很困惑为什么观察者UIResponder.keyboardWillShowNotification没有被调用。我正在键盘扩展的KeyboardViewController中使用它。 - Amit Verma

5

我只使用了UIResponder.keyboardWillHideNotification而未使用NSNotification.Name.。这在SWIFT 5中有效。

NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)

当我在键盘将要出现的通知上执行Command-Click并选择跳转到定义时。
extension UIResponder {

    
    public class let keyboardWillShowNotification: NSNotification.Name

    public class let keyboardDidShowNotification: NSNotification.Name

    public class let keyboardWillHideNotification: NSNotification.Name

    public class let keyboardDidHideNotification: NSNotification.Name

}

5

正在使用Swift 4.2进行开发。

 func bindToKeyboard(){
    NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name:
        UIApplication.keyboardWillChangeFrameNotification
        , object: nil)


}

4

以下是本答案补充的一些小细节:

func setupViews(){
            // Keyboard notification observers
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) 
    }


    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            // Do something
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        // Do something
    }

0
在Swift 4.2中更新后,您可以直接使用.UIKeyboardDidShow.UIKeyboardDidHide
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardDidShow, object: nil)

看起来这可能会引起问题?请参见https://stackoverflow.com/a/50915736/2227743 - Eric Aya

-3

@Krunal....移除 'NSNotification.Name',它就可以工作了


1
这是与此其他答案相同的解决方案。 - Eric Aya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接