swift - 在导航栏中的UI开关发生变化时在标题中更改标签

swift - 在导航栏中的UI开关发生变化时在标题中更改标签

问题描述:

虽然我还没有成功,但我想更改标题中的标签以与我的UISwitch 相关联吗?swift - 在导航栏中的UI开关发生变化时在标题中更改标签

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

嘿什么是self.header? –

+0

UICollectionViewCell位于保存标签的UICollectionViewController的顶部 – bennypalms661

+0

您能提供此View Controller的完整代码吗? –

试试这个:

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() 

}