Swift/UISwitch: how to implement a delegate/listener

后端 未结 7 1653
慢半拍i
慢半拍i 2020-12-29 19:24

In my UITableViewController I have a custom cell which contains a switcher which is the following:

import Foundation
import UIKit

class SwitchCell: UITableV         


        
7条回答
  •  生来不讨喜
    2020-12-29 20:02

    Swift 3:

    @IBOutlet weak var mySwitch: UISwitch!  
    
    mySwitch.addTarget(self, action: #selector(MyClass.switchChanged(_:)), for: UIControlEvents.valueChanged)
    
    func switchChanged(_ mySwitch: UISwitch) {
        if mySwitch.isOn {
            // handle on
        } else {
            // handle off
        }
    }
    

提交回复
热议问题