【问题标题】:Swift 3 NSNotificationCenter Keyboardwillshow/hideSwift 3 NSNotificationCenter 键盘将显示/隐藏
【发布时间】:2016-06-15 02:52:27
【问题描述】:

我有一段在 Swift 2 中工作的代码,我尝试使用 Xcode 将代码更新到最新版本,我修复了所有问题,除了两个问题。

我有这个代码:

let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

与此配对:

func keyboardWillShow(notification: NSNotification) {

    constraint.constant = -100
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification) {

    constraint.constant = 25
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

在第一部分我现在收到一个错误提示

类型“LoginViewController”没有成员“keyboardWillShow/Hide”

我不明白为什么它没有看到下面的方法。

有人知道这个问题的解决方案吗?

【问题讨论】:

  • 你是否在 viewDidLoad() 或 viewDidAppear() 方法中添加了 NotificationCenter?
  • LoginViewController.keyboardWillShow(_:)改成LoginViewController.keyboardWillShow(notification:)?
  • 试过了,xCode 想让我把 _ 添加回来
  • 试试func keyboardWillHide(_ notification: NSNotification) {#selector(LoginViewController.keyboardWillHide(_:))。注意keyboardWillHide 函数中添加的下划线。
  • @RubberDucky4444 查看updated Swift Programming book。页面 1027 和 1028 可能是您要查找的内容,您还可能需要在您的班级中添加 @objc(keyboardWillHideWithNotification:)

标签: ios swift nsnotificationcenter swift3


【解决方案1】:

查看更新后的Swift Programming Language book。第 1027 和 1028 页是您要查找的内容。应该是这样的:

func keyboardWillHide(_ notification: NSNotification) {…

请注意上面的附加下划线。另外:

#selector(LoginViewController.keyboardWillHide(_:))

您可能还需要将@objc(keyboardWillHideWithNotification:) 添加到您的班级。

【讨论】:

    【解决方案2】:

    在 Swift 4.2 上,NSNotificationCenter 的 addObserver 名称也发生了变化:

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

    【讨论】:

      【解决方案3】:

      使用适用于 swift3 的代码

      您可以使用您的 ViewController(例如,loginvc)添加通知

      let loginvc : LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
      
          NotificationCenter.default.addObserver(self,
              selector: #selector(loginvc.keyboardWillShow(notification:)),
              name: NSNotification.Name.UIKeyboardWillShow, object: nil)
          NotificationCenter.default.addObserver(self,
              selector: #selector(loginvc.keyboardWillHide(notification:)),
              name: NSNotification.Name.UIKeyboardWillHide, object: nil)
      

      然后添加键盘隐藏和显示方法

      func keyboardWillShow(notification: NSNotification) {
          if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
              print("Show") 
          }
      }
      func keyboardWillHide(notification: NSNotification) {
          if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
              print("Hide")
          }
      }
      

      【讨论】:

        【解决方案4】:

        NSNotificationCenter 对获取显示键盘进行了更改:

        NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        

        【讨论】:

          猜你喜欢
          • 2017-06-02
          • 1970-01-01
          • 2016-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多