11

我有一段在 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”

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

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

4

4 回答 4

10

查看更新的Swift 编程语言书。第 1027 和 1028 页是您要查找的内容。它应该是这样的:

func keyboardWillHide(_ notification: NSNotification) {…

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

#selector(LoginViewController.keyboardWillHide(_:))

您可能还需要添加@objc(keyboardWillHideWithNotification:)到您的课程中。

于 2016-06-15T10:06:41.027 回答
5

在 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)
于 2018-11-01T00:30:16.197 回答
4

使用适用于 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")
    }
}
于 2016-12-01T07:56:02.170 回答
1

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)
于 2016-10-17T13:51:12.993 回答