ios – 如何使用string中的参数创建Selector

ios – 如何使用string中的参数创建Selector,第1张

概述我正在使用 Swift 3.1和Xcode 8.3.3编写程序.我想创建一个类,负责在键盘出现和消失时移动整个视图.但是我在使用字符串参数创建自定义选择器时遇到了困难.要显示或隐藏键盘我们需要功能: func keyboardWillShow(notification: Notification) {//Code moving view when keyboard appears} 我正在尝试 我正在使用 Swift 3.1和Xcode 8.3.3编写程序.我想创建一个类,负责在键盘出现和消失时移动整个视图.但是我在使用字符串参数创建自定义选择器时遇到了困难.要显示或隐藏键盘我们需要功能:
func keyboarDWillShow(notification: Notification) {//Code moving vIEw when keyboard appears}

我正在尝试创建一个这样的选择器:

let selector = Selector(("keyboarDWillShow")NotificationCenter.default.addobserver(vIEw,selector: selector,name: .UIKeyboarDWillShow,object: anyVIEw.vIEw.window)

它正在编译,但是当键盘出现时,它会崩溃.
因为它是独立的类我不能使用这种结构:

#selector(keyboarDWillShow)

因为它将Swift函数转换为Objective-C函数(添加@objc).
所以问题是:如何用参数字符串创建一个Selector表单?

P. S.我可以将整个代码放在那里,但我不希望问题非常大,所以我会编辑问题,如果有人问…

解决方法

Here is what you want,Selector with string type and Notification parameter argument

斯威夫特4

NotificationCenter.default.addobserver(self,selector: Selector(("showKeyboard:")),name:NSNotification.name.UIKeyboarDWillShow,object: nil)var keyboardHeight = 0.0//-------------------@objc func showKeyboard(_ sender: Notification) {    keyboarDWillShow(sender: sender as NSNotification,adjustHeight: 150)    print("sender - \(sender)")}//-------------------func keyboarDWillShow(sender: NSNotification,adjustHeight: CGfloat) {    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {        keyboardHeight = Double(keyboardSize.height)        // do your calculations    }}

斯威夫特3

NotificationCenter.default.addobserver(vIEw,selector: Selector(("keyboarDWillShow:")),object: anyVIEw.vIEw.window)func keyboarDWillShow(_ notification: Notification) {        keyboarDWillShow(sender: sender as NSNotification,adjustHeight: 150)        print("sender - \(sender)")}

根据语言支持,这是普通的选择器
斯威夫特4

NotificationCenter.default.addobserver(self,selector: #selector(self.showKeyboard(sender:)),object: nil)var keyboardHeight = 0.0    //-------------------    @objc func showKeyboard(sender: NSNotification) {        keyboarDWillShow(sender: sender,adjustHeight: 150)    }    //-------------------    func keyboarDWillShow(sender: NSNotification,adjustHeight: CGfloat) {        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {            keyboardHeight = Double(keyboardSize.height)            // your operations here        }    }

斯威夫特3

NotificationCenter.default.addobserver(self,object: nil)var keyboardHeight = 0.0    //-------------------    func showKeyboard(sender: NSNotification) {        keyboarDWillShow(sender: sender,adjustHeight: 150)    }   //-------------------    func keyboarDWillShow(sender: NSNotification,adjustHeight: CGfloat) {        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {            keyboardHeight = Double(keyboardSize.height)            // your operations here        }    }
总结

以上是内存溢出为你收集整理的ios – 如何使用string中的参数创建Selector全部内容,希望文章能够帮你解决ios – 如何使用string中的参数创建Selector所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100564.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存