简体   繁体   English

无法识别的选择器发送到实例Swift NSNotification

[英]unrecognized selector sent to instance Swift NSNotification

I watched this video on a Messaging App for Swift and copied it word for word. 我在Swift的Messaging App上观看了此视频,然后逐字复制。 Yet, there have been some updates to Xcode recently so I am not sure if that is the problem or I typed something in wrong. 但是,最近对Xcode进行了一些更新,所以我不确定这是否是问题所在,或者我输入了错误的内容。 I also looked at other questions similar to mine and still can't find the issue. 我也看了其他类似我的问题,但仍然找不到问题。

The code I putting in the viewDidLoad() deals with the showing the keyboard: 我在viewDidLoad()中放入的代码用于显示键盘:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown", name: UIKeyboardDidShowNotification, object: nil)

Then I call the keyboardWasShow function here: 然后我在这里调用keyboardWasShow函数:

 func keyboardWasShown(notification: NSNotification) {

    let dict : NSDictionary = notification.userInfo!
    let s : NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let rect : CGRect = s.CGRectValue()

    UIView.animateWithDuration(0.3, delay: 0, options: .CurveLinear, animations: {

        self.resultsScrollView.frame.origin.y = self.scrollViewOriginalY - rect.height
        self.frameMessageView.frame.origin.y = self.frameMessageOriginalY - rect.height

        var bottomOffset : CGPoint = CGPointMake(0, self.resultsScrollView.contentSize.height - self.resultsScrollView.bounds.size.height)

        self.resultsScrollView.setContentOffset(bottomOffset, animated: false)

        }, completion: {
            (finished: Bool) in

            //

    })

}

The reason why I think it's the keyboardWasShown function because output showed keyboardWasShown]: unrecognized selector sent to instance 0x7f848c12be40 我之所以认为这是keyboardWasShown函数的原因,因为输出显示了keyboardWasShown]:无法识别的选择器已发送到实例0x7f848c12be40

Any help would be appreciated! 任何帮助,将不胜感激!

The method you implemented takes an argument so you need to add a colon to the end of the selector name. 您实现的方法带有一个参数,因此您需要在选择器名称的末尾添加一个冒号。

 let center = NSNotificationCenter.defaultCenter()
 center.addObserver(self, 
                    selector: "keyboardWasShown:",
                    name: UIKeyboardDidShowNotification,
                    object: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM