2017-04-09 1 views
0

퀴즈 기능을 작동 시키려고하고 있습니다. 내 문제는 두 번째보기 컨트롤러에서 점수를 인쇄하려고한다는 것입니다. 시뮬레이터를 실행할 때 사용자는 두 번째보기 컨트롤러로 이동하지만 점수는 인쇄되지 않습니다.신속하게 작동하도록 퀴즈 기능을 시도하는 중

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. 
    } 
} 

두 번째 뷰 컨트롤러 당신은 변수 글로벌 문자열로 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 로컬 변수가 있습니다. 두 변수가 다르다는 것을 인정하지만 변수 이름을 심각하게 고려해야합니다.

또한이 로컬 변수의 값을 변경하고 no는 전역 변수의 값을 변경합니다. 대부분의 경우 변수가 비슷한 이름을 가지기 때문입니다.

그냥 제안, 전역 변수를 사용하여 데이터를 전달하지 마십시오. VC간에 데이터를 전달하는 대리인 프로토콜 방식을 살펴보십시오.

관련 문제