Как скрыть несколько кнопок - это массив кнопок? - PullRequest
1 голос
/ 03 мая 2019

Как скрыть несколько кнопок в массиве кнопок? Мне нужно сделать это: если количество вопросов меньше, чем массив кнопок, то кнопки, которые имеют дополнительные функции, должны скрываться.

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

}

Ответы [ 2 ]

4 голосов
/ 03 мая 2019

Во-первых, я бы предложил использовать 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)
         }
     }

}
3 голосов
/ 03 мая 2019

Просто скройте все кнопки, затем прокрутите ответы на вопрос и обновите соответствующую кнопку соответственно

func updateTittles(_ question: Question?) {
    let arrayOfButton = [firstButton, secondButton, thirdButton, fourthButton, fifthButton, sixthButton]

    questionLabel.text = question?.text

    //1. Hide all
    arrayOfButton.forEach { (button) in
        button.isHidden = true
    }

    //2. Loop through question's answers and set relative button accordingly
    question?.answers.enumerated().forEach { (idx, answer) in
        let button = arrayOfButton[idx]
        button.isHidden = false
        button.setTitle(answer.text, for: .normal)
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...