gpt4 book ai didi

Swift 特定的通知观察者初始化

转载 作者:行者123 更新时间:2023-11-28 12:17:57 25 4
gpt4 key购买 nike

在尝试使用新的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com