swift - 我需要帮助调试此错误 - child 数学测验

标签 swift xcode

我的学校项目是一个 child 数学游戏,测试他们的加法、减法和乘法。我有两个与该错误相关的 VC。我一直遇到的错误是,每次我选择任何选项时,它都会忽略所有其他操作并遵循乘法指令!我已经尝试了一切,但我对这个应用程序感到沮丧。

选项_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
}

}

测验_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()
}

}

最佳答案

当您点击其中一个按钮(加、减或乘)时,应用程序会立即执行 prepareForSegue,然后再执行与这些按钮相关的操作。因此,addition_truesubtraction_truemultiplication_true 都始终为 false,并且代码会进入 else 子句,该子句设置乘法选项。

要解决这个问题:

  1. 在 Storyboard中删除三个按钮的转场。
  2. 添加从第一个 View Controller 到第二个 View Controller 的 Segue(控制从第一个 View Controller 顶部的黄色圆圈拖动到第二个 View Controller 的主体)。
  3. 给segue一个名称(突出显示segue并在属性检查器中将其命名(“mainSegue”或其他名称))
  4. 在您的代码中,将以下行添加到三个操作(addition_enter 等)中每个操作的末尾:

    performSegueWithIdentifier("mainSegue", 发件人: self)

这将确保在执行按钮操作后调用 segue。

关于swift - 我需要帮助调试此错误 - child 数学测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39055476/

相关文章:

macos - Playground 中未声明的类型 'NSView'

ios - 是否可以使用 AVAudioEngine 播放 iOS 音乐库中的歌曲?

swift - 如何更改 Facebook 确认对话框项目名称?

objective-c - 从 Objective-C 到 Swift 4 的一行代码

xcode - altool 应该如何使用 keychain 选项?

ios - 无法访问已安装的 Pod 类中的我的项目类

objective-c - [selfdismissModalViewControllerAnimated :YES]; 后如何调用 viewDidLoad

css - 为什么我的表格不会在我的网站上使用 Bootstrap 与页面中心对齐,但在 codepen 上可以吗?

XCode:堆栈 View 和约束

ios - 进入后台时暂停WKWebView?