2017-08-31 70 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. 
    } 
} 

希望這會有所幫助。