caleb
caleb

Reputation: 119

Question about disabling one button when one button is active in Swift

enter image description here

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

Upvotes: 0

Views: 245

Answers (3)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119128

You forgot to change the backgroundColor in the first condition of the first method. 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:

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. So there is only one source of truth now. If it works somewhere, it works everywhere.

Upvotes: 3

Rahul Parikh
Rahul Parikh

Reputation: 532

set left button default selected from 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

        }

Upvotes: 0

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

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

Upvotes: 0

Related Questions