我正在尝试使用单独的类来处理键盘通知。
类KeyboardHandling:
class KeyboardHandling{
class func addObserverKeyboardWillShow(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification){
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
print(duration)
}
}
MainVC:
class MainVC: UIViewController {
//MARK: Outlets
@IBOutlet weak var textInput: UITextField!
@IBOutlet weak var bottomToolbar: UIToolbar!
@IBOutlet weak var toolbarBottonConstraint: NSLayoutConstraint!
//MARK: Variables
var toolBarIntialBottomConstraint: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
toolBarIntialBottomConstraint = toolbarBottonConstraint.constant
KeyboardHandling.addObserverKeyboardWillShow()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
当 addObserverKeyboardWillShow() 不类函数时,该实现非常有效。
否则我收到错误
有人可以解释为什么一样吗?