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

标签 swift xcode swift3

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

相关文章:

ios - 图像的 UIImagePNG 表示和十六进制表示

ios - "Application failed codesign verification."- 在我更改 Xcode 中的一些build设置之前一直在工作

ios - 如何根据 swift 3 中的 Collection View 高度动态增加 TableView 高度?

ios - ios 11.0.3无法上传图片

ios - 核心数据&解码: Go one hierarchy step further in json

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

iphone - "Dirty"和 "Resident"与虚拟内存相关是什么意思?

ios - 对象的显示方式因 iOS 设备而异

ios - 当我们在 IOS 10.2 中滑过它时,UISwitch 不工作

ios - 更新 UITableViewCell 内的 UILabel 框架