ios - How to access variables which located in another class -


i'm trying access var located in class(viewcontroller), cannot access answeredcorrectly variable in lastview class. how can access , when call answeredcorrectly that(marked 1) going use 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()         // additional setup after loading view, typically nib.         answerbox.addtarget(self, action: "textfielddidchange:", forcontrolevents: uicontrolevents.editingchanged)         }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can 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){...}   } 

there couple of things do, if want utilize inheritance, go ahead , try kind of structure:

class viewcontroller : uiviewcontroller {   //the var want access   var answeredcorrectly: int = viewcontroller().answeredcorrectly   //your other code   //.... } 

then, inherit class, since class lastview inherits viewcontroller, class inherits viewcontroller have access uiviewcontroller.

note

if haven't changed subclass of viewcontroller, should uiviewcontroller default.

let's inherit class lastview class:

class lastview : viewcontroller {   //now lastview class inherits viewcontroller, inherits   //from uiviewcontroller, it's big chain of classes   @iboutlet weak var numberlabel: uilabel!    func assignlabeltocount() {     numberlabel.text = "\(answeredcorrectly)"   } } 

the function assigns variable answeredcorrectly, located in viewcontroller.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -