2017-04-09 11 views
0

クイズ機能を動作させようとしています。私の問題は、2番目のビューコントローラでスコアを印刷しようとしていることです。私がシミュレータを実行すると、ユーザーは2番目のView Controllerに移動しますが、スコアは印刷されません。クイズ機能を迅速に実行しようとしています

import UIKit 

var CurrentQuestion = "" // global variable to be accessed 

class ViewController: UIViewController { 

    // create an array to store queations 
    let questions = ["favourite pet?", "favourite colour", "where was i born"] 

// multiple arrays that contain strings, right anwser is first 
let anwsers = [["dog", "cat", "bird"], ["blue", "black", "green"], ["tokyo", "tennese", "england"]] 

// variables 
// keeps track of what question were at 
var currentQuestion = 0 
var rightAnwserPlacement:UInt32 = 0 // where right anwser is located 
var points = 0;  // counts number of points - score 



//lbl 
@IBOutlet weak var lbl: UILabel! 

//button 
@IBAction func btn(_ sender: UIButton) { 
    if (sender.tag == Int(rightAnwserPlacement)) 
    { 
     print ("right") 
     points += 1 
    } 
    else 
    { 
     print ("wrong") 
    } 

    if (currentQuestion != questions.count) 
    { 
     newQuestion() 
    } 
    else 
    { 
     performSegue(withIdentifier: "showScore", sender: self) 
    } 

} 


override func viewDidAppear(_ animated: Bool) { 

    newQuestion() 
} 






// function that displays a new question      sets up interface for the new questions 
func newQuestion() 
{ 
    lbl.text = questions[currentQuestion] 

    rightAnwserPlacement = arc4random_uniform(3)+1 // random button 

    // create a button 
    var button:UIButton = UIButton() 
    var x = 1 
    for i in 1...3 
    { 
     // create a button 
     button = view.viewWithTag(i) as! UIButton 

     if (i == Int(rightAnwserPlacement))   // checks to see if it matches the right anwser 
     { 
      button.setTitle(anwsers[currentQuestion][0], for: .normal) 
     } 
     else { 
      button.setTitle(anwsers[currentQuestion][x], for: .normal) 
      x = 2 

     } 

    } 

    currentQuestion += 1 
} 






override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

第2のビューコントローラは、変数のグローバル文字列としてCurrentQuestionを設定している

import UIKit 
class second: UIViewController { 

    @IBOutlet weak var label: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 


} 
override func viewDidAppear(_ animated: Bool) { 
    label.text = CurrentQuestion 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 


    // Pass the selected object to the new view controller. 
    } 
    */ 

} 
+0

ここで** ** CurrentQuestionを設定していますか?そして、 'Int'' currentQuestion'と' String'' CurrentQuestion'変数を混同してしまいます。最後に、コントローラ間でグローバルを使用してデータを渡さないでください。 – vadian

答えて

0

で、後に1によってその値をインクリメントしてみてください?それはどういう意味ですか?さらに、クラスには、currentQuestionという名前のローカル変数があります。 2つの変数が異なることは認められていますが、変数の名前を変更することを真剣に考えてください。

また、このローカル変数の値を変更していて、グローバル変数の値を変更していない場合は、noを変更します。おそらく、このミックスアップは、同様の変数の命名のためです。

また、グローバル変数を使用してデータを渡すことは避けてください。 VC間でデータを渡す代理人プロトコルの方法を調べてください。

関連する問題