快速通过编码按钮/功能发送数据

Posted

技术标签:

【中文标题】快速通过编码按钮/功能发送数据【英文标题】:Sending data through coded button/function in swift 【发布时间】:2020-07-21 21:22:25 【问题描述】:

我有一个为tableViewCell页脚创建的按钮,并且想在按下按钮时传递该部分,但是当我打印出该部分时,它是一个完全任意的数字,当按钮被按下了吗?

  func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? 
            
            
            let button = UIButton()
            button.setTitle("Add Set", for: .normal)
            button.backgroundColor = UIColor.lightGray
            button.layer.cornerRadius = 5
            button.addTarget(self, action: #selector(CurrentSessionVC.addSetPressed(section:)), for: .touchUpInside)
            
            return button
        


    @objc func addSetPressed(section: Int)
            //prints 4349567072
        

【问题讨论】:

【参考方案1】:

您可以使用tag 属性(在任何UIView 对象中都可用)和addTarget(_:action:for:) 对所有UIControl 对象都可用的事实将发送者(此处为您的按钮实例)传递给选择器:

//...
let button = UIButton()
button.tag = section // or any other int
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
//...

@objc func buttonAction(_ sender: UIButton) 
    let section = sender.tag

您可以阅读更多关于目标-行动机制here

【讨论】:

以上是关于快速通过编码按钮/功能发送数据的主要内容,如果未能解决你的问题,请参考以下文章

django功能八

django功能八

通过 PayPal Buy Now 按钮的自定义字段发送一个 json 编码的 var

firebase 功能电子邮件发送问题

PrimeNG - 通过按钮单击发送表单数据和文件上传数据

sh 使用geeknote快速捕获数据并将其发送到Evernote的功能