swift - How to pass data backwards to a view controller after passing forward? -


im developing quiz app , there second view controller appears after initial view controller asked answer question. on second view controller user must press button return initial view controller asked question. when segue second view controller believe new instance of initial view controller being created , user asked questions have answered. code in swift file initial view controller constructed when once user asked question once question removed array it's index. how make new instance of initial view controller isn't created segueing second view controller? or there way second view controller can access same methods initial view controller?

here code initial view controller:

import uikit  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)          questiontimer.text = string(counter)         }           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 = []      }     randomquestion()  }  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:

import uikit      class continuescreen: uiviewcontroller {     //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() } 

what need, sir delegate or unwind segue. far prefer delegates because they're easier understand , think bit more powerful.

in continuescreen view controller define protocol similar this:

protocol quizcompleteddelegate {     func completedquiz()     func canceledquiz() } 

also in continuescreen view controller need declare optional delegate of type quizcompleteddelegate

var delegate: quizcompleteddelegate? 

... , use delegate call functions when appropriate:

@ibaction func didpresscontinue(_ sender: any) {     if allquestionsanswered == true {         delegate.completedquiz()     } else {         delegate.cancelledquiz()     } } 

in initial view controller implement functions protocol:

extension viewcontroller: quizcompleteddelegate {     func completedquiz() {         //handle quiz complete     }     func cancelledquiz() {         //handle quiz cancelled     } } 

then last thing need set delegate of continuescreen view controller in prepareforsegue function of initial view controller.

override func prepare(for segue: uistoryboardsegue, sender: any?) {     if segue.identifier == "showcontinuescreen" {         let continuevc = segue.destination as! continuescreen         continuevc.delegate = self     } } 

remove call randomquestion() on initial view controller's viewdidappear, , you're in business!


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 -