ios - How do I make a picker view populate from an array of NSStrings? How would I then access the value of the item picked? -
okay, need picker user can select list of predefined options. can give me easy, simplified version of how populate picker view array of nsstrings? then, how read value picker? i'm noticing things nameofpicker.value , nameofpicker.text not work here.
thank you!
i have read following documents , don't understand getting at. https://developer.apple.com/library/ios/documentation/userexperience/conceptual/uikituicatalog/uipickerview.html https://developer.apple.com/library/ios/documentation/general/conceptual/cocoaencyclopedia/delegatesanddatasources/delegatesanddatasources.html#//apple_ref/doc/uid/tp40010810-ch11
it quite similar how populate data in uitableview
, setting datasource , delegate.
1. first step set delegate of picker view. in .m
file set datasource , delegate
@interface yourviewcontroller () <uipickerviewdatasource, uipickerviewdelegate> { } @property (strong, nonatomic) uipickerview *yourpickerview;
2. assign datasource , delegate
self.yourpickerview.delegate = self; self.yourpickerview.datasource = self;
3. implement datasource , delegate
// returns number of 'columns' display. - (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview { return 1; } // returns # of rows in each component.. - (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component { return self.yourarrayofstrings.count; } //the title should shown in picker view - (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component { nsstring *yourtitle = [self.yourarrayofstrings objectatindex:row] return yourtitle; } //this called when picker view value selected - (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { nslog @(@"selected row = %@",[self.yourarrayofstrings objectatindex:row]); }
Comments
Post a Comment