【问题标题】:Question about disabling one button when one button is active in Swift关于在 Swift 中一个按钮处于活动状态时禁用一个按钮的问题
【发布时间】:2020-01-27 10:32:51
【问题描述】:

上图有两个按钮。 按下左键后,背景被填充为红色。

如果我在这里按右键 我希望右键的背景填充为红色,左键为白色作为右键,并且按钮被停用。

override func viewDidLoad() {
        super.viewDidLoad()

        bookTitleFilterBtn.addTarget(self, action: #selector(bookTitleFilterBtnClicked(_:)), for: .touchUpInside)
        authorNameFilterBtn.addTarget(self, action: #selector(authorNameFilterBtnClicked(_:)), for: .touchUpInside)
    }
//left button
@objc func bookTitleFilterBtnClicked(_ sender: UIButton) {
        DispatchQueue.main.async {
            if self.isHighlighted == false {
                sender.backgroundColor = .red
                let title = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
                sender.setAttributedTitle(title, for: .normal)
                sender.isHighlighted = true
                self.isHighlighted = true
            } else {
                sender.backgroundColor = .white
                let title = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
                sender.setAttributedTitle(title, for: .normal)
                sender.isHighlighted = false
                self.isHighlighted = false
            }
        }
    }

//right button
    @objc func authorNameFilterBtnClicked(_ sender: UIButton) {
        DispatchQueue.main.async {

            if self.isHighlighted == false {
                sender.isHighlighted = true
                let title = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
                sender.setAttributedTitle(title, for: .normal)
                sender.backgroundColor = .red
                self.isHighlighted = true
            } else {
                sender.isHighlighted = false
                self.isHighlighted = false
                let title = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
                sender.setAttributedTitle(title, for: .normal)
                sender.backgroundColor = .white
            }
        }
    }

【问题讨论】:

标签: ios swift


【解决方案1】:

您忘记更改第一种方法的第一个条件中的backgroundColor。为了防止更多此类问题,请尝试在函数中定义逻辑并在需要的任何地方调用它,而不是一遍又一遍地重写它:

override func viewDidLoad() {
    super.viewDidLoad()

    bookTitleFilterBtn.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
    authorNameFilterBtn.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
}

var buttons: [UIButton] { return [bookTitleFilterBtn, authorNameFilterBtn] }

func updateButtonsAppearance(allButtons: [UIButton], selectedButton: UIButton) {
    for button in allButtons {
        let isSelected = button == selectedButton

        let currentTitle = button.currentTitle ?? "-"
        let title = NSAttributedString(string: currentTitle, attributes: [.foregroundColor: isSelected ? UIColor.white : UIColor.black])
        button.setAttributedTitle(title, for: .normal)
        button.backgroundColor = isSelected ? .red : .white
        button.isHighlighted = isSelected
    }
}

@objc func buttonClicked(_ sender: UIButton) {
    DispatchQueue.main.async {
        self.updateButtonsAppearance(allButtons: buttons, selectedButton: sender)
    }
}

请注意,两个按钮现在都调用相同的函数。所以现在只有一个真相来源。如果它在某处有效,那么它在任何地方都有效。

【讨论】:

  • 哦不!我刚刚解决了``` let title = NSAttributedString(string: button.titleLabel?.text ?? "", attributes: [.foregroundColor: isSelected ? UIColor.white : UIColor.black]) button.setAttributedTitle(title, for: .正常)```
  • 好!但使用currentTitle 而不是titleLabel?.text。按钮的不同状态可能会有所不同。
  • 我可以在选择按钮时返回一个字符串值吗? I want to return "book_title" when the left button is selected and "author_name" when the right button is selected.
  • 您可以使用“枚举”,但 cmets 不是讨论问题的好去处。随意提出一个新问题。 ;)
【解决方案2】:

您缺少以下代码行来更改另一个按钮的背景颜色。添加以下代码行。

//left button
@objc func bookTitleFilterBtnClicked(_ sender: UIButton) {
    DispatchQueue.main.async {
        if self.isHighlighted == false {
            ....
            ....
            authorNameFilterBtn.backgroundColor = .white

        } else {

            ....
        }
    }
}

//right button
@objc func authorNameFilterBtnClicked(_ sender: UIButton) {
    DispatchQueue.main.async {

        if self.isHighlighted == false {

            ....

            bookTitleFilterBtn.backgroundColor = .white
        } else {

            ....
        }
    }
}

此代码将改变按钮的颜色,反之亦然

【讨论】:

    【解决方案3】:

    设置从 StoryBoard 中选择的默认左按钮

    var selectedButton:String = "" // gloable variable 
    
    //left button
        @objc func bookTitleFilterBtnClicked(_ sender: UIButton) {
    
            if selectedButton != "제목"
            {
                selectedButton = "제목"
                sender.backgroundColor = .red
                let title = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])//title
                sender.setAttributedTitle(title, for: .normal)
                sender.isHighlighted = true
                self.isHighlighted = true
    
                authorNameFilterBtn.backgroundColor = .white
                let title1 = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])//title
                authorNameFilterBtn.setAttributedTitle(title1, for: .normal)
                authorNameFilterBtn.isHighlighted = false
                self.isHighlighted = false
            }
        }
    
        //right button
        @objc func authorNameFilterBtnClicked(_ sender: UIButton) {
    
            if selectedButton != "작가"
            {
                selectedButton = "작가"
                sender.isHighlighted = true
                let title = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])//Author
                sender.setAttributedTitle(title, for: .normal)
                sender.backgroundColor = .red
                self.isHighlighted = true
    
                bookTitleFilterBtn.isHighlighted = false
                self.isHighlighted = false
                let title1 = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
                bookTitleFilterBtn.setAttributedTitle(title1, for: .normal)
                bookTitleFilterBtn.backgroundColor = .white
    
            }
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2020-01-04
      • 1970-01-01
      • 2016-04-06
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多