我的学校项目是一个儿童数学游戏,它可以测试它们,减法和乘法。我有两个与错误相关的VC。我一直存在的错误是,每当我选择任何选项时,它都会忽略所有其他操作,并遵循乘法指令!我已经尝试了一切,我对这个应用程序感到沮丧。
Option_VC
class Option_VC: UIViewController {
var addition_true: Bool = false
var subtract_true: Bool = false
var multiply_true: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if addition_true == true {
let nextVC: Quiz_VC = segue.destinationViewController as! Quiz_VC
nextVC.addition_true = true
}else if subtract_true == true {
let nextVC: Quiz_VC = segue.destinationViewController as! Quiz_VC
nextVC.subtract_true = true
}else {
let nextVC: Quiz_VC = segue.destinationViewController as! Quiz_VC
nextVC.multiply_true = true
}
}
@IBAction func addition_enter(sender: AnyObject) {
addition_true = true
multiply_true = false
subtract_true = false
}
@IBAction func subtract_enter(sender: AnyObject) {
subtract_true = true
addition_true = false
multiply_true = false
}
@IBAction func multiply_enter(sender: AnyObject) {
multiply_true = true
addition_true = false
subtract_true = false
}
}
Quiz_VC
class Quiz_VC: UIViewController {
@IBOutlet var n1_lbl: UILabel!
@IBOutlet var back: UIButton!
@IBOutlet var next: UIButton!
@IBOutlet var enter: UIButton!
@IBOutlet var answer_field: UITextField!
@IBOutlet var symbol_lbl: UILabel!
@IBOutlet var n2_lbl: UILabel!
@IBOutlet var comment_lbl: UILabel!
@IBOutlet var score_lbl: UILabel!
var addition_true: Bool = false
var subtract_true: Bool = false
var multiply_true: Bool = false
var enter_entered_true: Bool = false
var answer: UInt32 = 0
var finalanswer: UInt32 = 0
var n1: UInt32 = 0
var n2: UInt32 = 0
var count = 0
var score = 0
var temp: UInt32 = 0
var operation: String = ""
override func viewDidLoad() {
super.viewDidLoad()
back.hidden = true
next.hidden = true
if addition_true == true {
AdditionQuestions()
}else if subtract_true == true {
SubtractionQuestions()
}
if multiply_true == true && addition_true == false && subtract_true == false{
MultiplicationQuestions()
}
}
func Operation() {
if addition_true == true {
operation = "1"
}else if subtract_true == true {
operation = "2"
}else {
operation = "3"
}
switch operation {
case "1":
finalanswer = n1 + n2
case "2":
finalanswer = n1 - n2
case "3":
finalanswer = n1 * n2
default: break
}
}
func AdditionQuestions() {
n1 = arc4random_uniform(9)+1
n2 = arc4random_uniform(9)+1
n1_lbl.text = "\(n1)"
n2_lbl.text = "\(n2)"
symbol_lbl.text = "+"
score_lbl.text = ""
comment_lbl.text = ""
}
func SubtractionQuestions() {
n1 = arc4random_uniform(9)+1
n2 = arc4random_uniform(9)+1
symbol_lbl.text = "-"
if n2 > n1 {
temp = n1
n1 = n2
n2 = temp
}
n1_lbl.text = "\(n1)"
n2_lbl.text = "\(n2)"
}
func MultiplicationQuestions() {
n1 = arc4random_uniform(9)+1
n2 = arc4random_uniform(9)+1
symbol_lbl.text = "×"
n1_lbl.text = "\(n1)"
n2_lbl.text = "\(n2)"
}
func EndQuiz() {
if count > 3 {
enter.hidden = true
next.hidden = true
back.hidden = false
score_lbl.text = "Score: \(score)"
comment_lbl.text = "Completed Quiz"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func enter_entered(sender: AnyObject) {
if answer_field.text != nil {
enter_entered_true = true
answer = UInt32(answer_field.text!)!
count = count + 1
Operation()
if answer != finalanswer {
comment_lbl.text = "Incorrect"
} else {
comment_lbl.text = "Correct"
score = score + 1
}
} else {
comment_lbl.text = "Enter a number!"
}
enter.hidden = true
next.hidden = false
}
@IBAction func next_entered(sender: AnyObject) {
if addition_true == true {
AdditionQuestions()
comment_lbl.text = ""
}else if subtract_true == true {
SubtractionQuestions()
comment_lbl.text = ""
}
if multiply_true == true && addition_true == false && subtract_true == false{
MultiplicationQuestions()
comment_lbl.text = ""
}
enter.hidden = false
next.hidden = true
EndQuiz()
}
}
答案 0 :(得分:0)
在Quiz_VC
班级更新 viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
back.hidden = true
next.hidden = true
if addition_true == true {
AdditionQuestions()
}else if subtract_true == true {
SubtractionQuestions()
}else if multiply_true == true{
MultiplicationQuestions()
}
}
和
func Operation() {
if addition_true == true {
operation = "1"
}else if subtract_true == true {
operation = "2"
}else if multiply_true == true {
operation = "3"
}
switch operation {
case "1":
finalanswer = n1 + n2
case "2":
finalanswer = n1 - n2
case "3":
finalanswer = n1 * n2
default: break
}
}
也
@IBAction func next_entered(sender: AnyObject) {
if addition_true == true {
AdditionQuestions()
comment_lbl.text = ""
}else if subtract_true == true {
SubtractionQuestions()
comment_lbl.text = ""
}else if multiply_true == true{
MultiplicationQuestions()
comment_lbl.text = ""
}else{
enter.hidden = false
next.hidden = true
EndQuiz()
}
}
答案 1 :(得分:0)
当您点击其中一个按钮(添加,减去或相乘)时,应用程序会在执行与这些按钮关联的操作之前立即执行prepareForSegue
。因此,addition_true
,subtraction_true
和multiplication_true
都是假的,代码会落到设置乘法选项的else
子句中。
解决这个问题:
在您的代码中,将以下行添加到三个操作(addition_enter
等)的每一个的末尾:
performSegueWithIdentifier("mainSegue", sender: self)
这将确保在执行按钮操作后调用segue。
答案 2 :(得分:0)
我认为你的@IBAction函数没有被连接,或者没有被调用,因为按钮被链接到一个segue。
这意味着所有Bools在prepareForSegue中都是错误的。在这种情况下,该函数中的逻辑设置QuizVC用于乘法(最后一个else子句没有if限定符)。
所以,在故事板中保留segue。在对象检查器(Xcode右侧的面板)中填充segue标识符字符串。例如,使用ADDITION_SEGUE,MULTIPLY_SEGUE,SUBTRACTION_SEGUE。你不必使用大写和下划线,这只是我的习惯。
然后在prepareForSegue中:你可以告诉哪个segue触发了这个动作。在此基础上设置QuizVC上的属性。那么你在OptionVC中根本不需要那些Bools。
在prepareForSegue中:
if segue.identifier == "ADDITION_SEGUE" {
destinationViewController...
}
如何将枚举放入QuizVC而不是使用Bools:
enum QuizType {
case Addition, Subtraction, Multiplication
}
从长远来看,这可能会让事情变得容易一些。它有助于您管理状态,因为测验类型只是三者之一。虽然,如果Bools为你工作那么公平。