I'm trying to access a var which located in another class(ViewController), but I cannot access answeredCorrectly variable in LastView class. How can I access it and when I call answeredCorrectly like that(marked with 1) is it going to use the default instance of ViewController?
I tried that(LastView.swift)
import Foundation
import UIKit
class LastView: ViewController {
@IBOutlet weak var numberLabel: UILabel!
func assignLabelToCount(){
numberLabel.text = "\(answeredCorrectly)"
}
}
Whole View Controller
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
answerBox.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var questionShowing = ""
var answerForControl = 0
@IBAction func newButton() {
var question = getQuestion()
questionShowing = question.0
answerForControl = question.1
questionLabel.text = questionShowing
var timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("endGame"), userInfo: nil, repeats: false)
print()
}
func print(){
println("\(questionShowing) >>>>>> \(answerForControl)")
}
var answeredCorrectly = 0
func textFieldDidChange(textField: UITextField) {
var answerInInt = String(stringInterpolationSegment: answerForControl)
var answer: String? = String(answerInInt)
if answerBox.text == answer {
newButton()
answeredCorrectly++
answerBox.text = ""
} else {
}
}
func endGame(){
println("Count of correct answers: \(answeredCorrectly)")
answeredCorrectly = 0
LastView().assignLabelToCount()
performSegueWithIdentifier("toEnd", sender: nil)
}
func getQuestion() -> (String, Int){...}
}