ios - Make a UIButton inactive if no text is entered in a TextField in Swift -


so concept simple, have textfield , button labelled 'next'. next button disabled users if have not put in textfield. this, have run code:

 @ibaction func nextbutton(sender: uibutton) {      if textfield.text.isempty {          buttonlabel.userinteractionenabled = false      }  } 

this disables button want, problem is, if text entered in textfield, button still disabled. have tried adding "else" statement after "if" statement reverse i'm saying see if works, doesn't.

any appreciated.

nick

my code:

import uikit  class viewcontroller: uiviewcontroller, uitextfielddelegate, uipickerviewdatasource, uipickerviewdelegate {  func imageeffect() {      // set vertical effect background     var verticalmotioneffect : uiinterpolatingmotioneffect =     uiinterpolatingmotioneffect(keypath: "center.y",         type: .tiltalongverticalaxis)     verticalmotioneffect.minimumrelativevalue = -20     verticalmotioneffect.maximumrelativevalue = 20      // set horizontal effect background     var horizontalmotioneffect : uiinterpolatingmotioneffect =     uiinterpolatingmotioneffect(keypath: "center.x",         type: .tiltalonghorizontalaxis)     horizontalmotioneffect.minimumrelativevalue = -20     horizontalmotioneffect.maximumrelativevalue = 20      // create group background combine both     var group : uimotioneffectgroup = uimotioneffectgroup()     group.motioneffects = [horizontalmotioneffect, verticalmotioneffect]      // add both effects view background     mybackgroundview.addmotioneffect(group)  }  var datepickerview:uidatepicker = uidatepicker()  @ibaction func textfieldedited(sender: uitextfield) {      var datepickerview  : uidatepicker = uidatepicker()     datepickerview.datepickermode = uidatepickermode.date     sender.inputview = datepickerview     datepickerview.addtarget(self, action: selector("handledatepicker:"), forcontrolevents: uicontrolevents.valuechanged)      }  func handledatepicker(sender: uidatepicker) {     var dateformatter = nsdateformatter()     dateformatter.dateformat = "dd mmmm yyyy"     questiontextfield.text = dateformatter.stringfromdate(sender.date) }  var countrydata = ["united kingdom", "france", "spain", "germany", "berlin", "eygpt", "united states"]  var pickerview:uipickerview = uipickerview()  @iboutlet var progressstatus: uiprogressview!  @iboutlet var barimage: uiimageview!  @iboutlet var questionlabel: uilabel!  @iboutlet var buttonbarimage: uiimageview!  @iboutlet var buttonlabel: uibutton!  @iboutlet var mybackgroundview: uiimageview!  @iboutlet var questiontextfield: uitextfield!  let questions = ["where going?", "what doing there?", "when go?"]  var currentquestionindex = 0  let placeholder = ["country", "activity", "date"]  var currentplaceholderindex = 0  @ibaction func nextbutton(sender: anyobject) {      if currentquestionindex == 0 {          progressstatus.setprogress(0.333, animated: true)      } else if currentquestionindex == 1 {          progressstatus.setprogress(0.666, animated: true)      } else {          progressstatus.setprogress(1, animated: true)      }      // 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++         //progressstatus.setprogress(0.333, animated: true)         buttonlabel.settitle("next", forstate: uicontrolstate.normal)           // animate text questionlabel         uiview.animatewithduration(1.0, delay: 0.0, usingspringwithdamping: 0.9, initialspringvelocity: 0.5, options: nil, animations: {              self.questionlabel.center = cgpoint(x: -110 , y: 305 + 20)              }, completion: nil)      } else {          //add logic here run whenever user has answered questions.      }  }   override func viewdidload() {     super.viewdidload()     // additional setup after loading view, typically nib.      questiontextfield.delegate = self     pickerview.delegate = self     pickerview.datasource = self      // applies image effect image in mybackgroundimage     imageeffect()      // sets button text     buttonlabel.settitle("get started", forstate: uicontrolstate.normal)      // sets question text blank     questionlabel.text = ""       // sets placeholder text in text field     questiontextfield.placeholder = ""      // hides text field     questiontextfield.hidden = true      // hides image background text field     barimage.hidden = true      // sets progress bar nil     progressstatus.setprogress(0, animated: true)      if questiontextfield.text.isempty {          buttonlabel.userinteractionenabled = false     }  }  func textfielddidbeginediting(textfield: uitextfield) {       buttonlabel.userinteractionenabled = true       switch currentquestionindex {      case 0:         // todo: add uipickerview         self.view.addsubview(datepickerview)     case 2:         // todo: add uidatepicker         self.view.addsubview(datepickerview)      default:         println("default")     } }   // resigns keyboard when user presses return/next key on keyboard  func textfieldshouldreturn(textfield: uitextfield) -> bool {      questiontextfield.resignfirstresponder()      return true  }  func numberofcomponentsinpickerview(pickerview: uipickerview) -> int {      return 1 }  func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int {      return countrydata.count  }  func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! {     return countrydata[row]  }  func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) {      questiontextfield.text = countrydata[row]     pickerview.hidden = true  }     // resigns keyboard if user presses anywhere on screen override func touchesbegan(touches: set<nsobject>, withevent event: uievent) {      self.view.endediting(true) }   override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  } 

edit:

set mybutton.userinteractionenabled = false in viewdidload or viewwillappear.

set textfield's delegate.

and then

func textfielddidbeginediting(textfield: uitextfield) {    if !textfield.text.isempty        mybutton.enable = true     } else {        buttonlabel.enable = false    } } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -