Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3 NSNotificationCenter键盘将显示/隐藏_Ios_Swift_Nsnotificationcenter_Swift3 - Fatal编程技术网

Ios Swift 3 NSNotificationCenter键盘将显示/隐藏

Ios Swift 3 NSNotificationCenter键盘将显示/隐藏,ios,swift,nsnotificationcenter,swift3,Ios,Swift,Nsnotificationcenter,Swift3,我有一段代码可以在Swift 2中使用,我尝试使用Xcode将代码更新到最新版本,我修复了除了两个问题以外的所有问题 我有以下代码: let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC NotificationCenter.defaultCenter().addObserver(self, selector: #selector(Login

我有一段代码可以在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”

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

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

请查看更新的解决方案。第1027页和第1028页是您要查找的内容。应该是这样的:

func keyboardWillHide(_ notification: NSNotification) {…
请注意上面的附加下划线。此外:

#selector(LoginViewController.keyboardWillHide(_:))

您可能还需要向类中添加
@objc(keyboardWillHideWithNotification:)

n通知中心对get show键盘进行了更改:

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)

使用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")
    }
}

在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)

您是否在viewDidLoad()或viewDidAppear()方法中添加了NotificationCenter?将
LoginViewController.keyboardWillShow(:)
更改为
LoginViewController.keyboardWillShow(notification:)
?尝试了这一点,xCode希望我添加uu-inTry
func keyboardWillHide(u-notification:NSNotification){
#选择器(LoginViewController.keyboardWillHide(:)
。请注意keyboardWillHide函数中添加的下划线。@RuberDucky4444查看。第1027页和第1028页可能是您要查找的内容,您可能还必须在类中添加
@objc(KeyboardWillHideWidthNotification:)