ios - how to set the data in second component based on first component selection in UIPickerview -
i using uipicker 3 components. want if select row in first component on basis of selected component shows value of corresponding data in second component. used array first component value @"uk",@"us",@"eu".now want if user selected uk in first component want load uksize1=[nsarray arraywithobjects:@"1",@"2",@"3",@"4",@"5", nil]; in second component , if user selected in first component want eusize2=[nsarray arraywithobjects:@"11",@"22",@"33",@"44",@"55", nil];
load in second component. same eu value want load ussize3=[nsarray arraywithobjects:@"111",@"222",@"333",@"444",@"555", nil];
. want second component value changed based on first component , third component value fixed.
this code tried not getting exact value.
- (id)init { if (self = [super init]) { notestodisplayforkey = [nsarray arraywithobjects: @"uk", @"us", @"eu", nil]; scalenames = [nsarray arraywithobjects: @"11", @"22", @"33", @"555",@"666",@"777",nil]; scalevalue=[nsarray arraywithobjects: @"1" ,@"2", @"3", @"4",@"5",@"6", @"7",@"8",@"9", @"10",@"11",nil]; uksize1=[nsarray arraywithobjects:@"1",@"2",@"3",@"4",@"5", nil]; eusize2=[nsarray arraywithobjects:@"11",@"22",@"33",@"44",@"55", nil]; ussize3=[nsarray arraywithobjects:@"111",@"222",@"333",@"444",@"555", nil]; } return self; } - (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview { return 3; } - (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component { // returns switch (component) { case 0: return [notestodisplayforkey count]; case 1: if([selectedkey isequaltostring:@"uk" ]) { return [uksize1 count]; } else if([selectedkey isequaltostring:@"us"] ) { return [ussize3 count]; } else { return [eusize2 count]; } //return [scalenames count]; case 2: return [scalevalue count]; default:break; } return 0; } - (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component { switch (component) { case 0: return [notestodisplayforkey objectatindex:row]; case 1:if([selectedkey isequaltostring:@"uk" ]) { selectedscale = [scalenames objectatindex:row]; return selectedscale; } else if([selectedkey isequaltostring:@"us"] ) { selectedscale = [ussize3 objectatindex:row]; return selectedscale; } else { selectedscale = [eusize2 objectatindex:row]; return selectedscale; } case 2: return [scalevalue objectatindex:row]; default:break; } return nil; } - (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { // nslog(@"row %i selected in component %i", row, component); switch (component) { case 0: selectedkey = [notestodisplayforkey objectatindex:row]; return; case 1: // selectedscale = [scalenames objectatindex:row]; if([selectedkey isequaltostring:@"uk" ]) { selectedscale = [scalenames objectatindex:row]; return; } else if([selectedkey isequaltostring:@"us"] ) { selectedscale = [ussize3 objectatindex:row]; return; } else { selectedscale = [eusize2 objectatindex:row]; return; } return; case 2: selectedvalue = [scalevalue objectatindex:row]; return; default:break; } }
just check method .it work .
- (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { // nslog(@"row %i selected in component %i", row, component); switch (component) { case 0: selectedkey = [notestodisplayforkey objectatindex:row]; [pickerview reloadallcomponents]; return; case 1: // selectedscale = [scalenames objectatindex:row]; if([selectedkey isequaltostring:@"uk" ]) { selectedscale = [uksize1 objectatindex:row]; return; } else if([selectedkey isequaltostring:@"us"] ) { selectedscale = [ussize3 objectatindex:row]; return; } else { selectedscale = [eusize2 objectatindex:row]; return; } return; case 2: selectedvalue = [scalevalue objectatindex:row]; return; default:break; } }
Comments
Post a Comment