gpt4 book ai didi

ios - 如何隐藏多个按钮是按钮数组?

转载 作者:搜寻专家 更新时间:2023-11-01 05:51:45 27 4
gpt4 key购买 nike

如何隐藏按钮数组中的多个按钮?我需要这样做:如果问题的数量少于按钮数组,那么应该隐藏具有额外内容的按钮。

@IBOutlet var firstButton: UIButton!
@IBOutlet var secondButton: UIButton!
@IBOutlet var thirdButton: UIButton!
@IBOutlet var fourthButton: UIButton!
@IBOutlet var fifthButton: UIButton!
@IBOutlet var sixthButton: UIButton!

func generateQuestions() -> Question {
// MARK: - Questions
let questionOne = Question.init("questionOne?")
let questionTwo = Question.init("questionTwo")

// MARK: - Answers
let answerOne = Answer.init("answerOne", type: .python, nextQuestion: nil)
let answerTwo = Answer.init("answerTwo", type: .next, nextQuestion: questionTwo)
let answerThree = Answer.init("answerThree", type: .next, nextQuestion: nil)
let answerFour = Answer.init("answerFour", type: .next, nextQuestion: nil)
let asnwerFive = Answer.init("asnwerFive", type: .next, nextQuestion: nil)
let answerSix = Answer.init("answerSix", type: .next, nextQuestion: nil)

let answerOneQT = Answer.init("answerOneQT", type: .next, nextQuestion: nil)
let answerTwoQT = Answer.init("answerTwoQT", type: .next, nextQuestion: nil)

// MARK: - Put answers to question
questionOne.answers = [answerOne, answerTwo, answerThree, answerFour, asnwerFive, answerSix]
questionTwo.answers = [answerOneQT, answerTwoQT]
return questionOne
}

//MARK: - Update label and titles of Buttons
func updateTittles(_ question: Question?) {
let arrayOfButton = [firstButton, secondButton, thirdButton, fourthButton, fifthButton, sixthButton]
questionLabel.text = question?.text
firstButton.setTitle(question?.answers[0].text, for: .normal)
secondButton.setTitle(question?.answers[1].text, for: .normal)
thirdButton.setTitle(question?.answers[2].text, for: .normal)
fourthButton.setTitle(question?.answers[3].text, for: .normal)
fifthButton.setTitle(question?.answers[4].text, for: .normal)
sixthButton.setTitle(question?.answers[5].text, for: .normal)
}

}

最佳答案

首先我建议使用 IBOutletCollection喜欢

@IBOutlet var buttons: [UIButton]!

其次隐藏你所有的按钮

func hideAllbuttons() {

for button in self.buttons {
button.isHidden = true
}
}

那么updateTittles就会变成

func updateTittles(_ question: Question?) {
hideAllbuttons()

for index in 0..<question.answers.count {

if index < self.buttons.count {
self.buttons[index].isHidden = false
self.buttons[index].setTitle(question?.answers[index].text, for: .normal)
}
}

}

关于ios - 如何隐藏多个按钮是按钮数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55966995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com