ios 传递多个参数给 addTarget

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33498064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:10:23  来源:igfitidea点击:

Pass multiple parameters to addTarget

iosswiftuitableview

提问by Arti

In my UITableViewCellI have a button. And I want to add action to it by passing multiple parameters in cellForRowAtIndexPathmethod.

在我的UITableViewCell我有一个按钮。我想通过在cellForRowAtIndexPath方法中传递多个参数来为其添加操作。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:
        indexPath) as! CartTableViewCell
    cell.buyButton.addTarget(self, action: self.buyButton(indexPath, 2, 3 ,4 , 5, 6), forControlEvents: .TouchUpInside)
}

回答by Ankita Shah

May be you can do something like this

也许你可以做这样的事情

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:indexPath) as! CartTableViewCell
    cell.buyButton.tag = (indexPath.section*100)+indexPath.row
    cell.buyButton.addTarget(self, action: "btnBuy_Click:", forControlEvents: .TouchUpInside)
}

func btnBuy_Click(sender: UIButton) {
        //Perform actions here
     let section = sender.tag / 100
     let row = sender.tag % 100
     let indexPath = NSIndexPath(forRow: row, inSection: section)
     self.buyButton(indexPath, 2, 3 ,4 , 5, 6)
}

Create tag value according to you'r requirement and maintaint it's integrity too.

根据您的要求创建标签值并保持其完整性。

回答by Yalcin Ozdemir

If you need to pass string just use accessibilityHint of UIButton.

如果您需要传递字符串,只需使用 UIButton 的 accessibilityHint 即可。

Tag can only store Int so if anyone wants to pass string data can use this. However, this not a proper way of passing data!

标签只能存储 Int 所以如果有人想传递字符串数据可以使用它。但是,这不是传递数据的正确方式!

example:

例子:

button.accessibilityHint = "your string data"
button.addTarget(self, action: Selector(("buttonTapped")), for: UIControlEvents.touchUpInside)

func buttonTapped (sender: UIButton) {
   let section = sender.accessibilityHint
}

回答by sheetal

I think we should architect the code well. The hacks will just make your code hard to understand and buggy. Here is some suggestion.

我认为我们应该很好地构建代码。黑客只会使您的代码难以理解和出错。这里有一些建议。

Make a custom button class

制作自定义按钮类

class CustomButton:UIButton {
var name:String = ""
var object:MyObjectClassType?

convenience init(name: String, object: MyObjectClassType) {
   self.init(frame:CGRectZero)
   self.name =name
   self.object = object!
}}

Now add target to your button

现在将目标添加到您的按钮

let btn = your custom button
btn.name = “Your name”
btn.object = any object
btn.addTarget(self, action: #selector(myMethod(_:)), forControlEvents:.TouchUpInside)

Now simply get your values in the method call

现在只需在方法调用中获取您的值

  @IBAction func myMethod(sender:CustomButton) {
    print(sender.name)
}

回答by Michael

Easy!

简单!

1:Subclass UIButton:

1:子类UIButton

class IndexPathCellButton: UIButton {
    var indexPath:NSIndexPath?
}


2:Set your value:

2:设置你的价值:

...
cell.indexPathButton.addTarget(self, action: #selector(LocationSearchViewController.cellOptionButtonClick(_:)), forControlEvents: .TouchUpInside)
cell.indexPathButton.indexPath = indexPath // Your value
...


3:Retrieve your value:

3:检索你的价值:

@objc private func cellOptionButtonClick(sender: IndexPathCellButton) {
    print(sender.indexPath)
}

回答by Michael Kaminowitz

You can use the tagproperty on the UIButtons's titleLabeland UIImageViewto add additional parameters

您可以使用tagUIButtons 上的属性titleLabelUIImageView添加其他参数

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{
let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:indexPath) as! CartTableViewCell
cell.buyButton.tag = (indexPath.section*100)+indexPath.row
cell.buyButton.imageView.tag = 2
cell.buyButton.titleLabel.tag = 3
cell.buyButton.addTarget(self, action: "btnBuy_Click:", forControlEvents: .TouchUpInside)
}