swift - How to make a timer reset after button gesture in a different view controller? -


i developing quiz app user asked question @ random , must answer on initial view controller. if user picks correctly, second view controller appears contains button pops view controller off navigation stack , goes initial view controller finish other questions. however, have timer want reset (start @ 15s) every time second view controller popped , initial view controller appears next question. how accomplish task? have countdown timer code in swift file. need know how start scratch every time second view controller popped/removed.

here's code initial view controller:

import uikit  extension viewcontroller: quizcompleteddelegate {     func continuequiz() {          randomquestion()     } }   class viewcontroller: uiviewcontroller {       var questionlist = [string]()       func updatecounter() {         counter -= 1         questiontimer.text = string(counter)          if counter == 0 {             timer.invalidate()             wrongseg()             counter = 15         }     }       func randomquestion() {         //random question         if questionlist.isempty {             questionlist = array(qadictionary.keys)         }          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()          }     }       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 {               counter = 15             questiontimer.text = string(counter)         } 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)     } } 

here's code second view controller:

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

how make countdown timer reset after button push on different view controller?

  • so if can reference of yourview controller have setup timer can access counter variable previous view controller inside second viewcontroller , when pushed button reset 0.
  • if above approach not possible way create static variable , access anywhere , reset this:-
static var counter = 0 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -