swift uipickerview second component selectrow not working -


i have pickerview in swift 2 components. works except when want set row in second component selectrow. when user reloads app , have value set. first component selectrow works. here code:

    override func viewdidload() {     super.viewdidload()      print("in viewdidload")      detailarmy.loadarmy(detailarmy, armymember: detailitem)      mygeneralpicker.datasource = self     mygeneralpicker.delegate = self     // set general picker view current value     let general = generaltypeinfo()     picker1options = general.generaltype()     print("detail item general: \(detailarmy.general)")     if detailarmy.general == "" {         mygeneralpicker.selectrow(0, incomponent: 0, animated: false)         picker2options = general.generalinfo("none")     } else {         mygeneralpicker.selectrow(picker1options.indexof(detailarmy.general)!, incomponent: 0, animated: false)         picker2options = general.generalinfo(detailarmy.general)     }     print("detail item general type: \(detailarmy.generaltype) picker2options: \(picker2options)")     if detailarmy.generaltype == "" {         mygeneralpicker.selectrow(0, incomponent: 1, animated: false)     } else {            //mygeneralpicker.selectrow(picker2options.indexof(detailarmy.generaltype)!, incomponent: 1, animated: false)         mygeneralpicker.selectrow(4, incomponent: 1, animated: false)     } }     //mark: delegates , data sources func numberofcomponentsinpickerview(pickerview: uipickerview) -> int {     //println("in numberofcomponenetsinpickerview pickerview: \(pickerview)")     return 2 }  func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int {     if component == 0 {         return picker1options.count     } else {         return picker2options.count     } }  //mark: delegates func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string? {     //var dicerow = dice[row]     print("titleforrow row: \(row) component: \(component)")     if component == 0 {         return string(picker1options[row])     } else {         return string(picker2options[row])     } }  func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) {     if component == 0 {         generalpickinfo = picker1options[row]         print("pickerview general selected: \(generalpickinfo)")         let general = generaltypeinfo()         picker2options = general.generalinfo(picker1options[row])         pickerview.reloadallcomponents()         mygeneralpicker.selectrow(0, incomponent: 1, animated: false)     } else {         generalpicktype = picker2options[row]         print("pickerview general type selected: \(generalpicktype)")     } }  func pickerview(pickerview: uipickerview, viewforrow row: int, forcomponent component: int, reusingview view: uiview?) -> uiview {     print("viewforrow row: \(row) component: \(component)")     let pickgenerallabel = uilabel()     pickgenerallabel.textalignment = .center     if component == 0 {         pickgenerallabel.text = string(picker1options[row])     } else {         pickgenerallabel.text = string(picker2options[row])     }     return pickgenerallabel }  class generaltypeinfo {     func generaltype() -> [string] {         return ["none","c-in-c","subordinate","ally"]     }      func generalinfo(type: string) -> [string] {         if type == "none" {             return ["none"]         } else {             return ["none","brilliant","inert"]         }     }      func generaltypeindex(type: string) -> int {         let generaltypedict = ["none": 0, "c-in-c": 1, "subordinate": 2, "ally": 3]         return generaltypedict[type]!     }      func generalinfoindex(type: string) -> int {         let generalinfodict = ["none": 0, "brilliant": 1, "inert": 2]         return generalinfodict[type]!     } } 

in code have deliberately put value of 4 in line:

mygeneralpicker.selectrow(4, incomponent: 1, animated: false) 

the line above has correct code want run, put in because should crash application since there isn't 4th value. , ignores this.

anyone tell me i'm doing incorrectly this? thanks.

so, ask question, , find out answer..

the answer move selectrow viewdidappear.

i found solution here: uipickerview selectrow doesn't works

thanks


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -