首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift 5不工作教程类型“NSNotification.Name”中的方法没有成员“UIResponder”

Swift 5不工作教程类型“NSNotification.Name”中的方法没有成员“UIResponder”
EN

Stack Overflow用户
提问于 2020-11-04 17:16:18
回答 1查看 92关注 0票数 0

我在编程和迅捷方面非常新手,所以我在YouTube上学习教程。我编写了示例中的代码,但我遇到了一个bug。告诉我怎么才能修好它。在教程中,您应该有一个显示和消失的键盘。

代码语言:javascript
运行
复制
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
        }
    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-04 17:24:35

只需删除名称中的NSNotification.Name

代码语言:javascript
运行
复制
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)

它必须是这样的:

代码语言:javascript
运行
复制
 NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64677166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档