ios - Save UIPickerView selection to NSUserDefaults swift -
i trying save user's selection uipickerview nsuserdefaults. have found answers in obj-c not swift. here uipickerview code:
func numberofcomponentsinpickerview(pickerview: uipickerview) -> int { return 1 } func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int { return pickerdata.count } func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! { return pickerdata[row] } func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { race = pickerdata[row] }
and when press save button, supposed save selected nsuserdefaults. here saving code:
@ibaction func save(sender: anyobject) { var defaults: nsuserdefaults = nsuserdefaults.standarduserdefaults() defaults.setobject(race, forkey: "race") }
when try display text on label, shows blank:
override func viewdidload() { super.viewdidload() self.racelabel.text = defaults.objectforkey("race") as? string
let me know if question needs clarification, thanks.
update code specified below , work
func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { race = row }
every time select row, function called , argument of parameter row
represents row selected in uipickerview.
if assign pickerdata[row]
value race
, assigning value pickerdata array @ index number represented row selected in uipickerview object.
Comments
Post a Comment