ios - Cannot figure out how to set starting values for UIPickerView -
i'm using uipickerview
, section same, last time used. looked @ apple's documents on did not see setting sections.
my code
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. // initialize data _pickerdata = @[@"i", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"]; // connect data picker.datasource = self; picker.delegate = self; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } /* #pragma mark - navigation // in storyboard-based application, want little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // new view controller using [segue destinationviewcontroller]. // pass selected object new view controller. } */ - (ibaction)aok:(id)sender { [self.navigationcontroller popviewcontrolleranimated:yes]; // store data int i=0; i=[picker selectedrowincomponent:1]*10; i+=[picker selectedrowincomponent:1]; } - (ibaction)acancel:(id)sender { [self.navigationcontroller popviewcontrolleranimated:yes]; } // number of columns of data - (int)numberofcomponentsinpickerview:(uipickerview *)pickerview { return 2; } // number of rows of data - (int)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component { // compment contains col number return _pickerdata.count; } // data return row , component (column) that's being passed in - (nsstring*)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component { return _pickerdata[row]; } // catpure picker view selection - (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { // method triggered whenever user makes change picker selection. // parameter named row , component represents selected. } @end
yep, had same problem while developing trivia game. here's how solved issue.
after you've loaded data, use select row:
uipickerview * pickerview = [[uipickerview alloc] initwithframe:rect]; [pickerview setdelegate:self]; [pickerview setdatasource:self]; [view addsubview:pickerview]; //we're going select row line [pickerview selectrow:[_pickerdata objectatindex:0] incomponent:0 animated:yes];
i see said pickerview default last selected row (i think that's you're trying say)?
if that's case, store last selected row in nsuserdefaults (which persisted if user kills app).
whenever user navigates picker view, can this:
nsinteger lastselectedrow = [[nsuserdefaults standarduserdefaults] integerforkey:@"lastselectedrow"]; if (lastselectedrow) [pickerview selectrow:lastselectedrow incomponent:0 animated:yes]; else [pickerview selectrow:[_pickerdata objectatindex:0] incomponent:0 animated:yes];
Comments
Post a Comment