简体   繁体   English

将静态参数添加到Swift中的#selector

[英]Add static parameter to #selector in Swift

Is it possible to pass an int variable via a selector, eg #selector(run(1)) or #selector(run(2)) 是否可以通过选择器传递一个int变量,例如#selector(run(1))或#selector(run(2))

More context if necessary: 必要时有更多背景:

let button = UIBarButtonItem(title: "Run",
                             style: UIBarButtonItemStyle.Plain,
                             target: self,
                             action: #selector(run(1)))

After confirming to some iOS Developers, no you can't do this yet. 在向一些iOS开发者确认之后,你还不能做到这一点。

But there is an alternative. 但还有另一种选择。 You can receive the sender object in the action method. 您可以在操作方法中接收发件人对象。 You can add any property to the sender class. 您可以将任何属性添加到sender类。 And receive that in action method. 并接受该行动方法。

for example: 例如:

First approach 第一种方法

let button = UIBarButtonItem(title: "Run",
                                   style: .Plain,
                                   target: self,
                                   action: #selector(run(_:)))
button.tag = 1

And you can receive it like this 你可以像这样收到它

func run(sender: UIBarButtonItem) {
    let passedInteger = sender.tag
}

But it only work if the passed parameter is a single Integer. 但它只有在传递的参数是单个整数时才有效。 Here's how you can do it if you want to pass multiple parameter with any data type -> Look at Second Approach 如果您想要使用任何数据类型传递多个参数,请执行以下操作 - >查看第二种方法

Second Approach 第二种方法

Subclass UIBarButtonItem 子类UIBarButtonItem

class MyBarButtonItem: UIBarButtonItem {
    var passedParameter: String?
}

And receive it like this 并像这样收到它

let button = MyBarButtonItem(title: "Run",
                                   style: .Plain,
                                   target: self,
                                   action: #selector(run(sender:)))

button.passedParameter = "John Doe"

func run(sender: MyBarButtonItem) {
    // now you have the parameter
    let parameter = sender.passedParameter
}

As of https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md起

#selector is just typed safe way to declare your method. #selector只是用于声明方法的类型安全方式。 So it is all about method signature, you can't add static parameter to it 所以关于方法签名,你不能添加静态参数

For example 例如

let sel = #selector(UIView.insertSubview(_:atIndex:)) // produces the Selector "insertSubview:atIndex:"

No, but as Edward mentioned, it may be possible to pass values through the button itself. 不,但正如爱德华提到的,可以通过按钮本身传递值。

let button = UIBarButtonItem(title: "Run",
                                   style: UIBarButtonItemStyle.Plain,
                                   target: self,
                                   action: #selector(run(_:)))
button.tag = 1

..... .....

func run(sender: UIButton){
     doSomething(sender.tag)
}

passing values through tags is not recommended but this is a way 不推荐通过标签传递值,但这是一种方法

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

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