ios - How to use the selected value in UIPickerView -
new this, trying create simple app user selects birth year , corresponding variable shown. i'd @ibaction func get variable birthyear uipicker @ bottom. set datasource , delegate uipicker viewcontroller in connections inspector panel.
class viewcontroller: uiviewcontroller,uipickerviewdelegate { var years = ["1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999"] let nineone = "24" let ninetwo = "23" let ninethree = "22" let ninefour = "21" let ninefive = "20" let ninesix = "19" let nineseven = "18" let 0 = "17" @ibaction func get(sender: anyobject) { if birthyear <= 1991 { println(nineone) } else if birthyear == 1992 { println(ninetwo) }else if birthyear == 1993{ println(ninethree) } else if birthyear == 1994{ println(ninefour) } else if birthyear == 1995{ println(ninefive) } else if birthyear == 1996{ println(ninesix) } else if birthyear == 1997{ println(nineseven) } // current year minus 17 else if birthyear >= 1998 { println(zero) } } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func numberofcomponentsinpickerview(pickerview: uipickerview) -> int { return 1 } func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int{ return years.count } func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string!{ return years[row] } func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { var birthyear = years[row] }
if don't know how continue, here 2 tutorials:
- displaying result uilabel: http://makeapppie.com/tag/uipickerview-in-swift/
- displaying result in uitextfield: http://asanhussain.blogspot.ca/2012/11/uipickerview-in-uiactionsheet-for-iphone.html
pay attention implementation
func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { }
also, uitextfield implementation, needs assign uipickerview inputview of uitextfield.
=====================================================================
this follow-up. there're 3 minor things noticed in code.
first, should connect iboutlet of uipickerview view controller. in code, connect ibaction called (which think it's not function name) other view component.
second, didn't assign delegate of picker view current view controller. delegate methods won't called.
third, birthyear's type string. can't compared int directly.
here revised code work if connect pickerview in storyboard iboutlet. hope helps.
import uikit class viewcontroller: uiviewcontroller,uipickerviewdelegate { @iboutlet var pickerview: uipickerview! var years = ["1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999"] let nineone = "24" let ninetwo = "23" let ninethree = "22" let ninefour = "21" let ninefive = "20" let ninesix = "19" let nineseven = "18" let 0 = "17" override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. pickerview.delegate = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func numberofcomponentsinpickerview(pickerview: uipickerview) -> int { return 1 } func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int{ return years.count } func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string!{ return years[row] } func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { var birthyear = years[row] self.printyear(birthyear) } func printyear(birthyearstring: string) { if let birthyear = birthyearstring.toint() { if birthyear <= 1991 { println(nineone) } else if birthyear == 1992 { println(ninetwo) }else if birthyear == 1993{ println(ninethree) } else if birthyear == 1994{ println(ninefour) } else if birthyear == 1995{ println(ninefive) } else if birthyear == 1996{ println(ninesix) } else if birthyear == 1997{ println(nineseven) } // current year minus 17 else if birthyear >= 1998 { println(zero) } } } }
Comments
Post a Comment