簡體   English   中英

關於Swift中一鍵激活時禁用一鍵的問題

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

在此處輸入圖像描述

上圖有兩個按鈕。 按下左鍵后,背景被填充為紅色。

如果我在這里按右鍵,我希望右鍵的背景填充為紅色,左鍵為白色作為右鍵,並且該按鈕被停用。

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

您忘記在第一種方法的第一個條件中更改backgroundColor顏色。 為了防止更多此類問題,請嘗試在 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)
    }
}

請注意,兩個按鈕現在都調用相同的 function。 所以現在只有一個真相來源。 如果它在某個地方工作,它在任何地方都可以工作。

您缺少以下代碼行來更改另一個按鈕的背景顏色。 添加以下代碼行。

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

            ....
        }
    }
}

此代碼將更改按鈕的顏色,反之亦然

設置左鍵默認選擇 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