【问题标题】:No Method declared with Objective-C Selector for Notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification没有使用 Objective-C 选择器声明用于通知 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 的方法
【发布时间】:2016-07-21 20:06:39
【问题描述】:

在最近更新 Xcode 之后,以前可以工作的这段代码不再工作了。大多数 Selector(":") 都有自动更正,但此代码除外:

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}

标记错误:

没有使用 Objective C 选择器 'keyboardWillSHow:' 声明的方法

这张图片显示了不同的尝试,但都失败了。

这段代码的新语法是什么?

【问题讨论】:

    标签: swift keyboard uitextfield xcode7 swift2.2


    【解决方案1】:

    如下分配Selector

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);
    

    以及更新你想要的方法:

    func keyboardWillShow(notification: NSNotification) {
    
         //Update UI or Do Something
    
    }
    

    你可以为UIKeyboardWillHideNotification做同样的事情。

    【讨论】:

      【解决方案2】:

      Swift 3 示例:

      NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
      NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
      
      // MARK: - Actions
      
      @objc private func keyboardWillShow(notification: Notification) {
          print("keyboardWillShow called")
      }
      
      @objc private func keyboardWillHide(notification: Notification) {
          print("keyboardWillHide called")
      }
      

      【讨论】:

        【解决方案3】:

        swift 语法发生了变化。试试这个:

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(ClassThatHasTheSelector.keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil);
        

        【讨论】:

        • 它仍然标记错误:类型:'SignInViewController' 没有成员'KeyboardWillShow'。除非你没有更新你的 Xcode,否则这段代码可以工作。
        • 找不到那个方法。确保keyboardWillShow 在该类中可用并且名称匹配(我注意到错误消息大写键盘)。以大写字母开头的方法名称是非正统的。
        • keyBoardWillShow 没有大写:抱歉错字。仍然标记与上述相同的错误。
        • 试试 self.keyboardWillShow 而不是类名。
        • 我已经试过了。没用。我认为这是 Xcode 的新问题。
        【解决方案4】:

        我遇到了同样的问题,还发现你引用的类也必须是 NSObject 的子类(这不是必需的。Swift 中的情况)否则你会收到消息

        error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C"
        

        【讨论】:

          【解决方案5】:

          Swift 3 语法(就像上面的 Sohil 一样):

              func someMethod(sender: Any?) {
                ...
              }
          
              func someBlockCallingWithSelector() {
                someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged) 
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-07-09
            • 1970-01-01
            • 1970-01-01
            • 2015-06-10
            • 1970-01-01
            • 2013-02-04
            相关资源
            最近更新 更多