ios - How to get my countdown timer to reset to every time the view loads? -


in ios quiz app 15 seconds answer question. if answered correctly, second view controller appears dismissed click of button on second view controller , first view controller reappears new question answered. however, countdown timer starting click answer question , not when click button on second view controller dismiss second view controller. want countdown timer reset button on second view controller dismissed , shows original view controller new question. what's best way make happen?

here's code (first view controller):

import uikit  extension viewcontroller: quizcompleteddelegate {     func continuequiz() {         questiontimer.text = string(counter)     } }  class viewcontroller: uiviewcontroller {      //random image func     func randomimage() {         index = int(arc4random_uniform(uint32(questionimages.count)))         questionimage.image = questionimages[index]     }      var questionlist = [string]()      func updatecounter() {         counter -= 1         questiontimer.text = string(counter)          if counter == 0 {                         timer.invalidate()             wrongseg()         }     }      func randomquestion() {                 //random question         if questionlist.isempty {             questionlist = array(qadictionary.keys)             continuequiz()                     }          let rand = int(arc4random_uniform(uint32(questionlist.count)))         questionlabel.text = questionlist[rand]          //matching answer values go question keys         var choices = qadictionary[questionlist[rand]]!          questionlist.remove(at: rand)          //create button         var button:uibutton = uibutton()          //variables         var x = 1         rightanswerbox = arc4random_uniform(4)+1          index in 1...4         {             button = view.viewwithtag(index) as! uibutton              if (index == int(rightanswerbox))             {                 button.settitle(choices[0], for: .normal)             }             else {                 button.settitle(choices[x], for: .normal)                 x += 1                 }                randomimage()             continuequiz()         }     }      let qadictionary = ["who thor's brother?" : ["atum", "loki", "red norvell", "kevin masterson"], "what name of thor's hammer?" : ["mjolinr", "uru", "stormbreaker", "thundara"], "who father of thor?" : ["odin", "sif", "heimdall", "balder"]]      //wrong view segue     func wrongseg() {                 performsegue(withidentifier: "incorrectseg", sender: self)             }      //proceed screen     func rightseg() {             performsegue(withidentifier: "correctseg", sender: self)     }      //variables     var rightanswerbox:uint32 = 0     var index = 0      //question label     @iboutlet weak var questionlabel: uilabel!      //answer button     @ibaction func buttonaction(_ sender: anyobject) {         if (sender.tag == int(rightanswerbox))         {             rightseg()             print ("correct!")         }          if counter != 0 {          }         else if (sender.tag != int(rightanswerbox)) {             wrongseg()             print ("wrong!")             timer.invalidate()             questionlist = []         }     }      override func viewdidappear(_ animated: bool)     {         randomquestion()     }      //variables     var counter = 15      var timer = timer()      @iboutlet weak var questiontimer: uilabel!      override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.          timer = timer.scheduledtimer(timeinterval: 1, target:self, selector: #selector(viewcontroller.updatecounter), userinfo: nil, repeats: true)             } 

code second view controller:

import uikit  protocol quizcompleteddelegate {     func continuequiz() }  class continuescreen: uiviewcontroller {      var delegate: quizcompleteddelegate?      //correct answer label     @iboutlet weak var correctlbl: uilabel!      //background photo     @iboutlet weak var backgroundimage: uiimageview!      func backtoquiz() {         if let nav = self.navigationcontroller {             nav.popviewcontroller(animated: true)             }         else {             self.dismiss(animated: true, completion: nil)         }     }      @ibaction func `continue`(_ sender: any) {         backtoquiz()         delegate?.continuequiz()     }      override func viewdidload() {         super.viewdidload()     } } 

you can implement function viewdidappear() on first view controller, function automatically called when second viewcontroller being dismissed , therefore first seen (again).

so create function in first view controller:

override func viewdidappear(_ animated: bool) {     super.viewdidappear(animated)      // here put timer reset     // ... } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -