Swift自定义按钮扩展了Uibutton



我想扩展Uibutton类以制作自己的广播按钮。

class UIRadioButton : UIButton {
        var checked = CheckedEnum.unchecked
        
        enum CheckedEnum{
            case checked , unchecked
        }
    }

它是ViewController中的内部类。但是,当我想制作此按钮时,将操作发送以查看控制器,就像我往常一样,它行不通。这是我的按钮连接窗口:在此处输入图像描述而且通常是按钮连接窗口:在此处输入图像描述

在我看来,您应该在接收触摸事件时实现uitableViewCell的委托并调用其方法。示例:

protocol CellButtonDelegate: class {
    func cellButton(cell: UITableViewCell, didTouch button: UIButton)
}
class Cell: UITableViewCell {
    weak var delegate: CellButtonDelegate?
    @IBAction func buttonTouched(sender: UIButton) {
          delegate?.cellButton(cell: self, didTouch button: sender)
    }
}

然后,将视图控制器作为每个表视图单元格的代表并处理此事件。

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, 
  cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView. dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! Cell
        // configure cell
        cell.delegate = self
    }
}
extension ViewController: CellButtonDelegate {
    func cellButton(cell: UITableViewCell, didTouch button: UIButton) {
        // handle button touch event, save indexPath of the cell to alter representation of table view after reloading data etc.
    }
}

希望这会有所帮助。

最新更新