我想更改标题中的标签以与我的UISwitch
相关联
虽然我还没有成功?
setLeftNavButton()
中调用 viewDidLoad()
func setLeftNavButton() {
let switchControl=UISwitch()
//switchControl.isOn = true
//switchControl.setOn(true, animated: false)
switchControl.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: switchControl)
self.switchControl = switchControl
}
var switchControl: UISwitch?
func switchValueDidChange(){
guard let mySwitch = switchControl else { return }
if mySwitch.isOn {
header?.onlineOfflineStatusLabel.text = "on"
}
else {
header?.onlineOfflineStatusLabel.text = "off"
}
self.header?.reloadInputViews()
self.collectionView?.reloadData()
}
答案 0 :(得分:0)
试试这个:
func setLeftNavButton() {
let switchControl = UISwitch()
switchControl.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: switchControl)
self.switchControl = switchControl
}
@objc
func switchValueDidChange(_ sender: UISwitch){
if sender.isOn {
header?.onlineOfflineStatusLabel.text = "on"
} else {
header?.onlineOfflineStatusLabel.text = "off"
}
self.header?.reloadInputViews()
self.collectionView?.reloadData()
}