简体   繁体   English

关于Swift中一键激活时禁用一键的问题

[英]Question about disabling one button when one button is active in Swift

在此处输入图像描述

The picture above has two buttons.上图有两个按钮。 The background is filled in red with the left button pressed.按下左键后,背景被填充为红色。

If I press right button here I want the background of the right button to be filled with red, the left button to be white as the right button, and the button to be deactivated.如果我在这里按右键,我希望右键的背景填充为红色,左键为白色作为右键,并且该按钮被停用。

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
            }
        }
    }

You forgot to change the backgroundColor in the first condition of the first method.您忘记在第一种方法的第一个条件中更改backgroundColor颜色。 To prevent more of these kind of issues, try to define the logic in a function and call it anywhere you need instead of rewriting it over and over:为了防止更多此类问题,请尝试在 function 中定义逻辑并在需要的任何地方调用它,而不是一遍又一遍地重写它:

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

Note that both buttons are now calling same function.请注意,两个按钮现在都调用相同的 function。 So there is only one source of truth now.所以现在只有一个真相来源。 If it works somewhere, it works everywhere.如果它在某个地方工作,它在任何地方都可以工作。

You are missing following line of code to change backgroundColor of another button.您缺少以下代码行来更改另一个按钮的背景颜色。 Add following line of code.添加以下代码行。

//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 {

            ....
        }
    }
}

This code will change color of buttons vice-versa此代码将更改按钮的颜色,反之亦然

set left button default selected from StoryBoard设置左键默认选择 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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM