Swift 3 NSNotificationCenter 键盘将显示/隐藏 [英] Swift 3 NSNotificationCenter Keyboardwillshow/hide

查看:17
本文介绍了Swift 3 NSNotificationCenter 键盘将显示/隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段适用于 Swift 2 的代码,我尝试使用 Xcode 将代码更新到最新版本,除两个问题外,我修复了所有问题.

I have a piece of code that worked in Swift 2 and I tried using Xcode to update the code to the newest version and I fixed everything except two issues.

我有这个代码:

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()
    }
}

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

On the first part I now get an error saying

类型LoginViewController"没有成员keyboardWillShow/Hide"

Type 'LoginViewController' has no member 'keyboardWillShow/Hide'

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

I don't understand why it is not seeing the method underneath.

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

Does anybody know a solution to this issue?

推荐答案

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

Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:

func keyboardWillHide(_ notification: NSNotification) {…

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

Notice the additional underscore above. Also:

#selector(LoginViewController.keyboardWillHide(_:))

您可能还需要将 @objc(keyboardWillHideWithNotification:) 添加到您的类中.

You also might need to add @objc(keyboardWillHideWithNotification:) to your class.

这篇关于Swift 3 NSNotificationCenter 键盘将显示/隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆