首页 > 解决方案 > IOS Updating button title and updating array with button value?

问题描述

I am new to IOS Development and am implementing IOS QUIZ App.

How to update question and answers in QUIZ app.

Please check below my code this is how I am displaying.

 @IBOutlet weak var prevBtn: UIButton!
    @IBOutlet weak var nxtBtn: UIButton!
    @IBOutlet weak var qstnlbl: UILabel!

    @IBOutlet weak var answerABtn: UIButton!
    @IBOutlet weak var answerBBtn: UIButton!
    @IBOutlet weak var answerCBtn: UIButton!
    @IBOutlet weak var answerDBtn: UIButton!

    let questions = ["How many days are there in 1 year?", "Favorite color?", "Where was I born?"]
    let answers = [["360", "365", "384", "377"], ["blue", "black", "green", "orange"], ["Tokyo", "New York", "Tennessee", "rajahmundry"]]

    //Variables
    var currentQuestion = 0
    var rightAnswerPlacement:UInt32 = 0
    var points = 0;

    //Label
    @IBOutlet weak var lbl: UILabel!

    //Button
    @IBAction func action(_ sender: AnyObject)
    {
        if (sender.tag == Int(rightAnswerPlacement))
        {
            print ("RIGHT!")
            points += 1
        }
        else
        {
            print ("WRONG!!!!!!")
        }

        if (currentQuestion != questions.count)
        {
            newQuestion()
        }

    }

    @IBAction func preAction(_ sender: AnyObject)
    {


        if (currentQuestion != questions.count)
        {
            newQuestion()
        }
       currentQuestion =  currentQuestion - 1


    }
    @IBAction func nxtaction(_ sender: AnyObject)
    {
        if (currentQuestion != questions.count)
        {
            newQuestion()
        }

       currentQuestion =  currentQuestion + 1

    }

    override func viewDidAppear(_ animated: Bool)
    {
        newQuestion()
    }

    //Function that displays new question
    func newQuestion()
    {
        qstnlbl.text = questions[currentQuestion]

        rightAnswerPlacement = arc4random_uniform(3)+1

        //Create a button
        var button:UIButton = UIButton()

        var x = 1

        for i in 1...4
        {
            //Create a button
            button = view.viewWithTag(i) as! UIButton
            button.setTitle(answers[currentQuestion][x], for: .normal)
                            x = 2

        }
        currentQuestion += 1
    }

based on user selection answers has be to added to answerArray.

For Ex: if user selects in first question in answerABtn answer option "A " answerArray has to update with "A" value.

user can able to update answer

for Ex: user select 1st question answer option "b" answerArray has to update with "b" value.

please help me out to make this quiz app.

quiz app ui

enter image description here

标签: iosswiftxcode

解决方案


干得好:

import UIKit

class ViewController: UIViewController {

  var emptyArray : [String] = []

  override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .red

  }

  @objc func buttonAction(sender: UIButton!) {
    emptyArray.append("A")
    print(emptyArray)
  }

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let button = UIButton()
    button.frame = CGRect(x: self.view.frame.size.width - 300, y: 200, width: 100, height: 100)
    button.backgroundColor = UIColor.blue
    button.setTitle("Name your Button ", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    self.view.addSubview(button)
  }

}

在此处输入图像描述

当你一遍又一遍地按下按钮时,调试窗口的输出是这样的

["A"]
["A", "A"]
["A", "A", "A"]
["A", "A", "A", "A"]

推荐阅读