2017-08-31 66 views
-1

我想扩展UIButton类来创建我自己的单选按钮。swift自定义按钮扩展UIButton

class UIRadioButton : UIButton { 
     var checked = CheckedEnum.unchecked 

     enum CheckedEnum{ 
      case checked , unchecked 
     } 
    } 

它是Viewcontroller中的内部类。但是当我想让这个按钮发送动作来查看控制器时,就像我通常那样,它不起作用。这是我的按钮连接窗口: enter image description here 而且它通常是按钮连接窗口: enter image description here

+0

显示你的代码*“发送动作来查看控制器,就像我通常做的一样”* ......如果没有看到你在做什么,就不可能给你任何帮助。 – DonMag

回答

0

在我看来,你应该实现你的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. 
    } 
} 

希望这会有所帮助。