如何使用字符串中的参数创建选择器 [英] How to create Selector with parameters from string

查看:42
本文介绍了如何使用字符串中的参数创建选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swift 3.1 和 Xcode 8.3.3 编写程序.我想创建一个类,负责在键盘出现和消失时移动整个视图.但是我在创建带有字符串参数的自定义选择器时遇到了困难.要显示或隐藏键盘,我们需要函数:

I am writing a program using Swift 3.1 and Xcode 8.3.3. I want to create a class, responsible for moving entire view when keyboard appears and disappears. But I faced difficulties with creating custom Selector with parameters from string. To show or hide keyboard we need function:

func keyboardWillShow(notification: Notification) {
//Code moving view when keyboard appears
}

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

I am trying to create a selector like this:

let selector = Selector(("keyboardWillShow")
NotificationCenter.default.addObserver(view, selector: selector, name: .UIKeyboardWillShow, object: anyView.view.window)

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

It is compiling, but when keyboard appears, it crashes. Because it is independent class I cannot use this construction:

#selector(keyboardWillShow)

因为它将 Swift 函数转换为 Objective-C 函数(添加 @objc).那么问题来了:如何创建带有参数字符串的 Selector 表单?

Because it transforms Swift function to Objective-C function (adding @objc). So question is: how to create a Selector form with parameters string?

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

P. S. I can put the whole code there but I don't want question to be very big, so I will edit question if somebody asks...

推荐答案

这是你想要的,带有字符串类型和通知参数参数的选择器

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

Swift 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
    }
}


Swift 3

NotificationCenter.default.addObserver(view, selector: Selector(("keyboardWillShow:")), name: .UIKeyboardWillShow, object: anyView.view.window)

func keyboardWillShow(_ notification: Notification) { 
       keyboardWillShow(sender: sender as NSNotification, adjustHeight: 150)
        print("sender - \(sender)")

} 



这里是普通选择器,根据语言支持
Swift 4

NotificationCenter.default.addObserver(self, selector: #selector(self.showKeyboard(sender:)), name:NSNotification.Name.UIKeyboardWillShow, 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
        }
    }


Swift 3

NotificationCenter.default.addObserver(self, selector: #selector(self.showKeyboard(sender:)), name:NSNotification.Name.UIKeyboardWillShow, 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
        }
    }

这篇关于如何使用字符串中的参数创建选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆