ios - How to segue to a new UIViewController at the end of an array count -


let me first explain concept. ask user 3 questions, stored in nsarray. there 1 uitextfield user use write 3 answers questions.

what happen, when 3 questions have shown array, next time user presses next uibutton, segues new uiviewcontroller. have created segue in main.storyboard , given identifier of countdownsegue.

when run app, , last question , press next button, app crashes , error "fatal error: array index out of range" - don't understand why. code below show happening questions array , button when it's pressed. appreciated.

let questions = ["where going?", "which city?", "when go?"]  var currentquestionindex = 0  let placeholder = ["country", "city", "date"]  var currentplaceholderindex = 0  @ibaction func nextbutton(sender: anyobject) {      // initial setup on button press     questiontextfield.hidden = false     barimage.hidden = false     questiontextfield.placeholder = placeholder[currentplaceholderindex]     questionlabel.text = questions[currentquestionindex]     questiontextfield.resignfirstresponder()      // reset text field have no text     questiontextfield.text = ""      // displays questions in array , displays placeholder text in textfield     if currentquestionindex < questions.count && currentplaceholderindex < placeholder.count {          currentquestionindex++         currentplaceholderindex++         buttonlabel.settitle("next", forstate: uicontrolstate.normal)      } else if currentquestionindex > questions.count && currentplaceholderindex > placeholder.count {          performseguewithidentifier("countdownsegue", sender: self)      }  } 

thanks!

you should check index against count - 1 because index zero-based.

if currentquestionindex < questions.count - 1 && currentplaceholderindex < placeholder.count - 1{     currentquestionindex++     currentplaceholderindex++     buttonlabel.settitle("next", forstate: uicontrolstate.normal) } else {     performseguewithidentifier("countdownsegue", sender: self) } 

edit:

you didn't check index when getting values array display text. overflows when tap next button on last question.

@ibaction func nextbutton(sender: anyobject) {      // initial setup on button press     questiontextfield.hidden = false     barimage.hidden = false     questiontextfield.resignfirstresponder()      // reset text field have no text     questiontextfield.text = ""      // displays questions in array , displays placeholder text in textfield     if currentquestionindex < questions.count && currentplaceholderindex < placeholder.count {         questiontextfield.placeholder = placeholder[currentplaceholderindex]         questionlabel.text = questions[currentquestionindex]          currentquestionindex++         currentplaceholderindex++         buttonlabel.settitle("next", forstate: uicontrolstate.normal)      } else {         performseguewithidentifier("countdownsegue", sender: self)     } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -