首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift -当导航栏中的UI开关更改时,更改标题中的标签

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

Stack Overflow用户
提问于 2017-10-28 21:23:37
回答 1查看 766关注 0票数 0

我想更改标题中的标签以与我的UISwitch相关联,尽管到目前为止还没有成功?

viewDidLoad()中调用setLeftNavButton()

代码语言:javascript
复制
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()

}
EN

回答 1

Stack Overflow用户

发布于 2017-10-28 22:57:54

试试这个:

代码语言:javascript
复制
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()

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46990203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档