如何使用选择器通过函数传递两个参数

tp2376

在中cellForRowAtIndexPath,我试图将开关选择和字符串传递给选择器功能,但我无法通过。请帮忙。

let pidName = "someString"
cell.switchSelection.tag = indexPath.row
    cell.switchSelection.addTarget(self, action: 
 #selector(self.switchOperation(_:pid:)), for: .valueChanged)

在这里,我必须通过发件人,pidName但失败

@objc func switchOperation(_ sender: UISwitch, pid: Any){
     let store = (sender).isOn
     let settingTag = pid
}
丹尼尔T.

正确的解决方案是将操作移至您的单元格类中:

class MyCell: UITableViewCell {
    @IBOutlet weak var switchSelection: UISwitch!

    override func awakeFromNib() {
        super.awakeFromNib()
        switchSelection.addTarget(self, action: #selector(MyCell.action(_:)), for: .valueChanged)
    }

    func configure(switchAction: @escaping (Bool) -> Void) {
        self.switchAction = switchAction
    }

    @objc func action(_ sender: UISwitch) {
        switchAction(sender.isOn)
    }

    private var switchAction: (Bool) -> Void = { _ in }
}

现在,在表视图委托中,您可以执行以下操作:

let pidName = "someString"
cell.configure(switchAction: { [weak self] isOn in 
    let store = isOn 
    let settingTag = pid
}

或者你可以这样做:

let pidName = "someString"
cell.configure(switchAction: { [weak self] isOn in 
    self?.switchOperation(isOn: isOn, pid: pid)
}

@objc func switchOperation(isOn: Bool, pid: Any){
    let store = isOn
    let settingTag = pid
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在日期时间选择器中使用两个验证器

如何保存两个日期选择器数据

如何在两个日期选择器之间使用验证?

如何使用水豚 + minitest 断言两个选择器之一

如何使用 javascript 或 jquery 比较两个时间选择器

如何使用选择器方法传递多个参数

Swift:如何通过我的 UIBarButtonItem 的选择器参数将我选择的 textField 作为参数传递

如何从两个不同的函数传递参数

通过CSS选择器选择两个已知元素之间的所有元素

在使用lambda的函数中传递两个参数

如何在C ++中使用单个模板参数传递两个Lambda函数

通过带有选择器的参数传递的快速调用函数

如何在选择选项上使用Javascript函数发送两个参数

快速选择器,其中两个参数未触发

如何在swiftui中交换两个选择器选择的值

如何将使用日期范围选择器获得的值转换为两个单独的日期和时间?

通过组件Angular 2中的选择器传递参数

将参数传递给Swift中的选择器函数

将函数参数传递给 Swift 中的选择器

在1个VC中使用两个UI选择器,处理委托方法

使用一个目标选择器指定两个元素

如何在Swift中将选择器函数作为参数传递?

如何将函数参数传递给jquery选择器

两个JavaScript参数如何传递“ this”

如何使用 max() 函数选择两个不同的组

:not()选择器中的两个类

合并两个attributeContains选择器

jQuery选择器有两个可能的值

两个jQuery选择器之间的联合