gpt4 book ai didi

swift - 当导航栏中的 UI 开关更改时更改标题中的标签

转载 作者:行者123 更新时间:2023-11-30 12:03:01 24 4
gpt4 key购买 nike

我想更改 header 中的标签以与我的 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()

}

最佳答案

试试这个:

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

}

关于swift - 当导航栏中的 UI 开关更改时更改标题中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com