xcode - Reload Component in UI Picker View Swift -


i unable reload second component in upickerview based on first component. tried using reloadallcomponents either not work or crashes. trying work on place car makes on left , models of cars on right. when car make changes, simultaneously change right component model.

class registrationpage: uiviewcontroller,uipickerviewdatasource, uipickerviewdelegate { @iboutlet weak var usernametextfield: uitextfield! @iboutlet weak var passwordtextfield: uitextfield! @iboutlet weak var passwordretypetextfield: uitextfield! @iboutlet weak var makepicker: uipickerview! @iboutlet weak var modelpicker: uipickerview! @iboutlet weak var licensetagtextfield: uitextfield! var picker1options = [] var picker2options = []  @ibaction func submitbutton(sender: anyobject) {  }  override func viewdidload() {     super.viewdidload()      makepicker = uipickerview()     modelpicker = uipickerview()     let makeandmodel = carmakemodel();      picker1options = makeandmodel.makevalues()     let firstvalue = picker1options.objectatindex(0)     picker2options = makeandmodel.modelvalues(firstvalue as! string)  }  func numberofcomponentsinpickerview(pickerview: uipickerview) -> int {     return 2 }  func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int {     if (component == 0) {         return picker1options.count     } else {         return picker2options.count     }  }  func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! {     if component == 0 {         return "\(picker1options[row])"     } else {         let makeandmodel = carmakemodel();         let currentvalue = picker1options.objectatindex(row)         var picker2ptions = makeandmodel.modelvalues(currentvalue as! string)         return "\(picker2ptions[row])"     } } func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) {     let makeandmodel = carmakemodel();     let currentvalue = picker1options.objectatindex(row)     var picker2ptions = makeandmodel.modelvalues(currentvalue as! string)     pickerview.reloadallcomponents()  } 

}

thanks!

here version works. created own carmakemodel class test it. overthinking titleforrow function primary problem. also, in didselectrow need update class property picker2options , not local variable. changed other things. i'd recommend compare version yours line line see differences.

also, there 1 pickerview needed, , should connected 1 in storyboard. shouldn't instantiating own pickerviews.

class carmakemodel {     func makevalues() -> [string] {         return ["toyota", "honda"]     }      func modelvalues(make: string) -> [string] {         if make == "toyota" {             return ["camry", "sienna", "highlander"]         } else {             return ["accord", "civic", "pilot"]         }     } }  class registrationpage: uiviewcontroller,uipickerviewdatasource, uipickerviewdelegate {     @iboutlet weak var usernametextfield: uitextfield!     @iboutlet weak var passwordtextfield: uitextfield!     @iboutlet weak var passwordretypetextfield: uitextfield!     @iboutlet weak var makepicker: uipickerview!     @iboutlet weak var modelpicker: uipickerview!     @iboutlet weak var licensetagtextfield: uitextfield!     var picker1options:[string] = []     var picker2options:[string] = []      @ibaction func submitbutton(sender: anyobject) {      }      override func viewdidload() {         super.viewdidload()          //makepicker = uipickerview()         //modelpicker = uipickerview()         let makeandmodel = carmakemodel();          picker1options = makeandmodel.makevalues()         let firstvalue = picker1options[0]         picker2options = makeandmodel.modelvalues(firstvalue)      }      func numberofcomponentsinpickerview(pickerview: uipickerview) -> int {         return 2     }      func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int {         if (component == 0) {             return picker1options.count         } else {             return picker2options.count         }      }      func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! {         if component == 0 {             return "\(picker1options[row])"         } else {             return "\(picker2options[row])"         }     }      func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) {         if component == 0 {             let makeandmodel = carmakemodel();             let currentvalue = picker1options[row]             picker2options = makeandmodel.modelvalues(currentvalue)             pickerview.reloadallcomponents()         }     } } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -