1

这是我添加观察者的功能

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}

.UIKeyboardWillShow给了我一个错误

'UIKeyboardWillShow' 已重命名为 'UIResponder.keyboardWillShowNotification'

将“UIKeyboardWillShow”替换为“UIResponder.keyboardWillShowNotification”

但是当我更换它时

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIResponder.keyboardWillShowNotification, object: nil)
}

我收到这个错误

在没有更多上下文的情况下,表达式的类型是模棱两可的

4

4 回答 4

5

没有一个点

NotificationCenter.default.addObserver(self, 
selector: #selector(keyboardWillShow), 
name: UIResponder.keyboardWillShowNotification, object: nil)
于 2018-09-17T13:03:58.607 回答
0

只需导入模块UIKit,因为它不是模块UIResponder的一部分UIKitFoundation

斯威夫特 5 代码

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addObservers()
    }

    public func addObservers() {
        NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    }

    @objc func handleKeyboardWillShow(_: Notification) {
        // Here handle keyboard
    }
}
于 2019-05-17T08:40:32.870 回答
0
let notificationCenter = NotificationCenter.default

notificationCenter.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) { (notification) in
                self.keyboardWillShow(notification: notification)
            }
notificationCenter.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { (notification) in
                self.keyboardWillHide(notification: notification)
            }
于 2018-10-10T10:23:29.787 回答
-1

我已经使用了这个,并且工作得很好

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowOrHide(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil);
于 2018-09-17T13:08:08.570 回答