【发布时间】: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