如何访问位于另一个类中的变量

时间:2015-06-25 12:13:53

标签: ios swift

我正在尝试访问位于另一个类(ViewController)中的var,但我无法在LastView类中访问answersCorrectly变量。我如何访问它,当我调用answersCorrectly(标记为1)是否将使用ViewController的默认实例?

我试过了(LastView.swift)

import Foundation
import UIKit



class LastView: ViewController {

    @IBOutlet weak var numberLabel: UILabel!

    func assignLabelToCount(){
        numberLabel.text = "\(answeredCorrectly)"
    }

}

全视图控制器

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


}

4 个答案:

答案 0 :(得分:0)

将变量numberLabel声明为public

答案 1 :(得分:0)

您需要创建该类的实例才能访问该变量。

前:

var lastViewInstance: LastView = LastView() // Declare in the class in which you want to access the variable

lastViewInstance.numberLabel.text = "Access from Another class"

这是您可以访问任何变量或插座的方法!

答案 2 :(得分:0)

您可以使用NSUserDefaults将对象存储到磁盘中,您可以这样使用它:

将您的对象存储到NSUserDefaults

NSUserDefaults.standardUserDefaults().setObject("YouObjectValue", forKey: "YourKey")

之后,您可以通过以下方式访问项目:

let yourVar: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("YourKey")

希望它会对你有所帮助。

答案 3 :(得分:0)

如果你想利用inheritance,你可以做几件事,继续尝试这种结构:

class ViewController : UIViewController {
  //the var you want to access
  var answeredCorrectly: Int = ViewController().answeredCorrectly
  //your other code
  //....
}

然后,inherit该类,因为您的班级LastView 继承 ViewController继承 ViewController的任何类现在可以访问UIViewController

注意

如果您尚未更改ViewController的子类,则默认情况下应为UIViewController

让我们继承您LastView班级的课程:

class LastView : ViewController {
  //now your LastView class inherits from ViewController, which also inherits
  //from UIViewController, it's like a big chain of classes
  @IBOutlet weak var numberLabel: UILabel!

  func assignLabelToCount() {
    numberLabel.text = "\(answeredCorrectly)"
  }
}

该功能只是简单地指定变量answeredCorrectly,该变量位于ViewController