【问题标题】:Toggle function with UIButton programmatically in swift在 swift 中以编程方式使用 UIButton 切换功能
【发布时间】:2017-09-05 01:07:32
【问题描述】:

我是 swift 的新手,我想用 UIButton 做一个切换/切换功能。

我尝试了多种方法,但没有找到解决方案。

请你帮我解决这个问题。

这是我的代码(这些按钮在 viewDidLoad 方法中声明):

let switchLightBT = UIButton()
switchLightBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102))
switchLightBT.backgroundColor = .clear
switchLightBT.tag = 1
switchLightBT.setImage(#imageLiteral(resourceName: "dark-icon"), for: .normal)
switchLightBT.addTarget(self, action: #selector(darkTheme(sender:)), for: .touchUpInside)
self.view.addSubview(switchLightBT)

let switchDarkBT = UIButton()
switchDarkBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102))
switchDarkBT.backgroundColor = .clear
switchDarkBT.tag = 2
switchDarkBT.setImage(#imageLiteral(resourceName: "light-icon"), for: .normal)
switchDarkBT.addTarget(self, action: #selector(lightTheme(sender:)), for: .touchUpInside)

self.view.addSubview(switchDarkBT)

和行动:

    func darkTheme(sender: UIButton!){
        self.view.backgroundColor = .black
sender.isHidden = true
        for button in self.wbButtons {
            button.setTitleColor(UIColor .white, for: .normal)
        }
    }

    func lightTheme(sender: UIButton!){
        self.view.backgroundColor = .white
sender.isHidden = true
        for button in self.wbButtons {
            button.setTitleColor(UIColor .black, for: .normal)
        }
    }

只是为了预览,没有此代码(也在 viewDidLoad 方法中声明),我希望工作正常,但 ONE TIME 并且我一直希望这个工作。

if switchDarkBT.isSelected == false {
    switchDarkBT.isHidden = false
    switchLightBT.isHidden = true
} else {
    switchDarkBT.isHidden = true
    switchLightBT.isHidden = false

}

【问题讨论】:

  • 什么是self.wbButtons
  • 它是多个按钮的插座集合。
  • 点击按钮后你想要什么?
  • 点击按钮时要一个隐藏一个取消隐藏按钮吗?
  • 嗨,我想隐藏/禁用“switchLightBT”并显示“switchDarkBT”,反之亦然。

标签: ios swift3 uibutton swift2 toggle


【解决方案1】:

试试这个

class ViewController: UIViewController { 
let switchLightBT = UIButton() 
let switchDarkBT = UIButton() 

override func viewDidLoad() { 
super.viewDidLoad() 
switchLightBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102)) 
switchLightBT.backgroundColor = .clear 
switchLightBT.tag = 1 
switchLightBT.setImage(UIImage(named:"light"), for: .normal) 
switchLightBT.addTarget(self, action: #selector(darkTheme(sender:)), for: .touchUpInside) 
self.view.addSubview(switchLightBT) 

switchDarkBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102)) 
switchDarkBT.backgroundColor = .clear 
switchDarkBT.tag = 2 
switchLightBT.setImage(UIImage(named:"dark"), for: .normal) 
switchDarkBT.addTarget(self, action: #selector(lightTheme(sender:)), for: .touchUpInside) 

self.view.addSubview(switchDarkBT) 

} 

func darkTheme(sender: UIButton!){ 
self.view.backgroundColor = .black 
switchLightBT.isHidden = true 
switchDarkBT.isHidden = false 

} 

func lightTheme(sender: UIButton!){ 
self.view.backgroundColor = .white 
switchLightBT.isHidden = false 
switchDarkBT.isHidden = true 

} 

【讨论】:

  • 是的,但是在第一次操作(第一次 touchUpInside)之后,所有按钮都会消失!我需要切换按钮 - 如果“switchDarkBT”被隐藏,则显示“switchLightBT”,反之亦然!
  • 您在哪里创建了这两个按钮?在 viewDidLoad 中?
  • 你在self.wbButtons中添加了这两个按钮吗?
  • 我想你不明白我想要什么!我不想隐藏按钮集合,我只想切换激活/停用我配置的颜色主题的两个按钮!当我按下“switchDarkBT”按钮时,它会激活颜色主题 A,并且相同的按钮会消失并出现“switchLightBT”按钮,依此类推...
  • switchDarkBT 和 switchLightBT 在 vi​​ewDidLoad 方法中创建
【解决方案2】:

您可以将 switchLightBT 和 switchDarkBT 声明为实例变量,并将这些行添加到您的按钮函数中。

func darkTheme(sender: UIButton!){
        switchDarkBT.isHidden = true
        switchLightBT.isHidden = false
        self.view.backgroundColor = .black
        for button in self.wbButtons {
            button.setTitleColor(UIColor .white, for: .normal)
        }
    }

func lightTheme(sender: UIButton!){
        switchLightBT.isHidden = true
        switchDarkBT.isHidden = false
        self.view.backgroundColor = .white
        for button in self.wbButtons {
            button.setTitleColor(UIColor .black, for: .normal)
        }
        }

【讨论】:

  • 像这样:var switchDarkBT = Bool() var switchLightBT = Bool()
  • 抱歉,像这样:var switchDarkBT = UIButton() var switchLightBT = UIButton()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 2015-03-21
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多