Swift 教程类型 'NSNotification.Name' 中的 5 没有工作方法没有成员 'UIResponder'
Swift 5 doesn't working method from tutorial Type 'NSNotification.Name' has no member 'UIResponder'
我在编程方面还很陌生 swift,所以我在 YouTube 上学习教程。我按照示例中的代码编写了代码,但是我 运行 遇到了错误。告诉我如何解决它。在教程中,你应该有一个出现和消失的键盘。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyBoardNotification()
}
deinit {
removeKeyBoardNotification()
}
@IBOutlet weak var scrollViewLogInScreen: UIScrollView!
@IBOutlet weak var topTextField: UITextField!
@IBOutlet weak var bottomTextField: UITextField!
@IBAction func logInButtonTapped(_ sender: UIButton) {
topTextField.resignFirstResponder()
bottomTextField.resignFirstResponder()
}
@objc
This code doesn't work with following errors:
Type 'NSNotification.Name' has no member 'UIResponder'
**func registerForKeyboardNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(kbWillHide), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}**
This code also doesn't work with following errors:
Type 'NSNotification.Name' has no member 'UIResponder'
**func removeKeyBoardNotification() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name:
NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}**
func kbWillShow(_ notification: Notification) {
let userInfo = notification.userInfo
let kbFrameSize = (userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
scrollViewLogInScreen.contentOffset = CGPoint.init(x: 0, y: kbFrameSize.height)
}
func kbWillHide() {
scrollViewLogInScreen.contentOffset = CGPoint.zero
}
}
只需删除名称
中的NSNotification.Name
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
一定是这样的:
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
我在编程方面还很陌生 swift,所以我在 YouTube 上学习教程。我按照示例中的代码编写了代码,但是我 运行 遇到了错误。告诉我如何解决它。在教程中,你应该有一个出现和消失的键盘。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyBoardNotification()
}
deinit {
removeKeyBoardNotification()
}
@IBOutlet weak var scrollViewLogInScreen: UIScrollView!
@IBOutlet weak var topTextField: UITextField!
@IBOutlet weak var bottomTextField: UITextField!
@IBAction func logInButtonTapped(_ sender: UIButton) {
topTextField.resignFirstResponder()
bottomTextField.resignFirstResponder()
}
@objc
This code doesn't work with following errors:
Type 'NSNotification.Name' has no member 'UIResponder'
**func registerForKeyboardNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(kbWillHide), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}**
This code also doesn't work with following errors:
Type 'NSNotification.Name' has no member 'UIResponder'
**func removeKeyBoardNotification() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name:
NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}**
func kbWillShow(_ notification: Notification) {
let userInfo = notification.userInfo
let kbFrameSize = (userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
scrollViewLogInScreen.contentOffset = CGPoint.init(x: 0, y: kbFrameSize.height)
}
func kbWillHide() {
scrollViewLogInScreen.contentOffset = CGPoint.zero
}
}
只需删除名称
中的NSNotification.Name
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
一定是这样的:
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)