ios - 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.
解决方案
干得好:
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"]
推荐阅读
- mysql - 计算不同折扣范围的产品
- c# - 如何防止 Guid.NewGuid() 在同一实例中生成第二个 guid?
- python-3.x - 如何使用 pandas read_excel 方法为 excel 文件创建权限?
- in-app-purchase - 这是什么原因?java.lang.IndexOutOfBoundsException
- php - 无法加载动态库 'php_<ext>.dll'
- excel - 针对不同单元格中的数据分析和评估字符串的宏
- java - 使用 Python 或 Java 从本地将数据上传到 Azure ADLS Gen2
- r - 确定矩阵是否在相同位置具有 NA(逻辑)
- android - Android Firestore 规则自定义身份验证
- paysafe - 使用 Paysafe 付款时遇到问题