【问题标题】: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
        }

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多