ios - Display questions from an array in order on a button press in swift -


i new learning ios swift, apologies if easy.

my apps first page has simple layout:

uilabel (my question) uitextfield (where user writes answer) uibutton (active once user has input text field)

i have made array of 4 questions. trying code when user presses uibutton, display next question in array, long text field has been populated.

this have far:

@iboutlet var textfield: uitextfield!  @iboutlet var questionlabel: uilabel!  @iboutlet var buttonlabel: uibutton!  let questions = ["where going?", "do know city?", "what doing there?", "when go?"]   @ibaction func nextbutton(sender: anyobject) {      textfield.hidden = false     textfield.placeholder = "country"       var = 0; < 5; i++ {         questionlabel.text = questions[0]     }      buttonlabel.settitle("next", forstate: uicontrolstate.normal)    }  override func viewdidload() {     super.viewdidload()     // additional setup after loading view, typically nib.      //keyboard     textfield.delegate = self      buttonlabel.settitle("get started", forstate: uicontrolstate.normal)     questionlabel.text = ""      // hides text field     textfield.hidden = true      // hides image background text field     barimage.hidden = true  } 

i able make when user presses 'get started' button, changes button text 'next', displays textfield , first question in array.

but i'm having trouble figuring out how move next question once user has inputted answer , pressed next button.

any appreciated.

nick

add property called currentquestionindex keep track of in array , increase when click button:

var currentquestionindex = 0  @ibaction func nextbutton(sender: anyobject) {      textfield.hidden = false     textfield.placeholder = "country"      questionlabel.text = questions[currentquestionindex]      if currentquestionindex < questions.count {          ++currentquestionindex          buttonlabel.settitle("next", forstate: uicontrolstate.normal)     } else {         (sender uibutton).userinteractionenabled = false     }  } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -