【问题标题】:Type 'NSNotification.Name' has no member 'keyboardDidShowNotification'类型“NSNotification.Name”没有成员“keyboardDidShowNotification”
【发布时间】:2019-02-18 08:44:15
【问题描述】:

我在使用 Swift 4.2 时遇到此错误

类型“NSNotification.Name”没有成员“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)

Apple 文档参考:NSNotification.Name.keyboardDidShowNotification

【问题讨论】:

  • 与您的“按钮类型”问题一样...@Krunal-这可能会有所帮助...(不是我的,我还没有尝试过...只是通过搜索找到的):@ 987654323@
  • @DonMag - 谢谢。这是很不错的。但看起来像是对 Swift4.2 的补丁/支持。我正在迁移我的完整项目。
  • 当然...但是该要点可能对于这些类型的语法更改是一个不错的参考。至于迁移整个项目,Xcode 10确实有一个迁移助手可能会有所帮助。参考:swift.org/migration-guide-swift4.2

标签: ios keyboard-events nsnotificationcenter swift4.2


【解决方案1】:

相信你现在是这样用的。

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 doc 中列为类型属性。

【讨论】:

  • 奇怪的是,.keyboardDidShowNotification 在UIWindow 和UIResponder 中都存在。
  • @rmaddy 实际上我从 xcode 得到了这个建议。但是当我转到通知页面的文档时,它显示在UIWindow 下。 ://
  • 奇怪的是,两个版本(UIWindow 和 UIResponder)都可以编译。
  • @rmaddy 我发现了我为什么这么想。 “许多关键对象也是响应者,包括 UIApplication 对象、UIViewController 对象和所有 UIView 对象(其中包括 UIWindow)” developer.apple.com/documentation/uikit/uiresponder
  • .keyboardDidShowNotification 实际上必须在UIResponder 中声明,因为我可以做UIView.keyboardDidShowNotification 或任何最终从UIResponder 继承的类。
【解决方案2】:

使用 swift 4,2

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


}

【讨论】:

    【解决方案3】:

    在这个答案中添加了一些小细节:

    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
        }
    

    【讨论】:

      【解决方案4】:

      我只使用了 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)
      

      当我命令点击keyboardWillShowNotification 并选择跳转到定义时。

      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 更新后,您可以直接使用 .UIKeyboardDidShow 和 .UIKeyboardDidHide

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

        【讨论】:

        【解决方案6】:

        @Krunal.... 删除 'NSNotification.Name' 它将起作用

        【讨论】:

        猜你喜欢
        • 2019-02-18
        • 2019-09-17
        • 2020-08-11
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-14
        相关资源
        最近更新 更多