在尝试使用新的 Swift NotificationCenter 时,我尝试将观察者对象创建为属性(反对将观察者分配给自身的经典 Obj-C 模式):
private let keyboardWillShowObserver = {
return NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil, using: self.keyboardWillShow(_:))
}()
private func keyboardWillShow(_ notification: Notification) {
bottomVerticalSpaceConstraint.constant = 400
}
问题是我收到以下错误消息,即使我有同一个类的成员函数部分:
Value of type '(NSObject) -> () -> MyAwesomeViewController' has no member 'keyboardWillShow'
最佳答案
HomeViewController 是您的 View Controller
private let keyboardWillShowObserver = {
return NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil, using: { (Notification) in
HomeViewController.keyboardWillShow (Notification)
})
}()
static func keyboardWillShow(_ notification: Notification) {
// bottomVerticalSpaceConstraint.constant = 400
}
关于Swift 特定的通知观察者初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45834793/