2016-08-20 45 views
0

我的學校項目是一個兒童數學遊戲,它除了測試他們,減法和乘法。我有兩個鏈接到錯誤的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

試試打印每個傳輸操作的布爾值, – Dravidian

回答

0

當你點擊按鈕(加,減或乘),那麼應用程序立即執行prepareForSegue它執行與這些按鈕相關的動作之前的一個。因此,addition_true,subtraction_truemultiplication_true都始終爲假,代碼將落入設置乘法選項的else子句。

要解決這個問題:

  1. 在故事板從三個按鈕刪除塞格斯。
  2. 從第一個視圖控制器添加一個segue到第二個視圖控制器(控制從第一個視圖控制器頂部的黃色圓圈拖動到第二個視圖控制器的主體)。
  3. 給這個SEGUE名稱(突出SEGUE並將其命名爲(「mainSegue」或東西)在屬性檢查器)
  4. 在代碼中添加以下行到三個動作結束(addition_enter,等):

    performSegueWithIdentifier("mainSegue", sender: self)

這將確保該按鈕的動作已經被執行後SEGUE被調用。

+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() 
    } 

    } 
0

我認爲你的@IBAction函數沒有掛鉤,或者沒有被調用,因爲按鈕鏈接到一個segue。

這意味着所有Bools在prepareForSegue中都是錯誤的。並且該函數中的邏輯在這種情況下爲乘法設置QuizVC(最後一個else子句沒有if限定符)。

所以,保持在故事板中的塞格。在對象檢查器中填充segue標識符字符串(Xcode中右側的面板)。使用例如ADDITION_SEGUE,MULTIPLY_SEGUE,SUBTRACTION_SEGUE。你不必使用大寫和下劃線,這只是我的習慣。

Segue Identifier

然後在prepareForSegue:你可以告訴我們賽格瑞觸發的操作。在此基礎上設置QuizVC上的屬性。那麼你在OptionVC中完全不需要這些Bools。

在prepareForSegue:

if segue.identifier == "ADDITION_SEGUE" { 
    destinationViewController... 
} 

如何把同樣一個枚舉到QuizVC而不是使用BOOLS:

enum QuizType { 
    case Addition, Subtraction, Multiplication 
} 

這可能使事情從長遠來看更容易一些。它有點幫助你管理狀態,因爲測驗類型只是三個之一。雖然如果Bools爲你工作的話,那就夠公平了。