Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Get Selected Value From UIPickerView With 2 Components
    text
    copied!<p>I have a question here where I asked how to get a value form UIPickerView ( <a href="https://stackoverflow.com/questions/5725580/how-to-get-selected-value-from-uipickerview">How To Get Selected Value From UIPickerView</a> ). I got a solution for a picker with 1 component but I'm trying to get the value from picker with 2 component now.</p> <p>This code works for picker with 1 component:</p> <pre><code> NSInteger row; NSString *weightSelected; row = [repPicker selectedRowInComponent:0]; weightSelected = [pickerArray objectAtIndex:row]; </code></pre> <p>I tired this code for my picker with 2 components, but it is freezing. There is no error in the console either. The line it is freezing at is <code>row2 = [repPicker selectedRowInComponent:1]; </code>:</p> <pre><code> NSInteger row1, row2; NSString *weightSelected1; NSString *weightSelected2; row1 = [repPicker selectedRowInComponent:0]; row2 = [repPicker selectedRowInComponent:1]; weightSelected1 = [pickerArray objectAtIndex:row1]; weightSelected2 = [pickerArray objectAtIndex:row2]; NSString *weightSelected = [NSString stringWithFormat:@"%@.%@", weightSelected1, weightSelected2]; </code></pre> <p>UPDATE:</p> <pre><code>#pragma mark - Weight Picker - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { if (thePickerView.tag==1) { return 2; } else return 1; } - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { if (thePickerView.tag==1) { if (component == 0) return [pickerArray count]; if (component == 1) return [pickerArrayHalf count]; } else return [pickerArray count]; } - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // NSLog(@"Selected Color: %@. Index of selected color: %i", [weightArray objectAtIndex:row], row); } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { if (pickerView.tag==1) { if (component == 0) { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)]; int weight = [[pickerArray objectAtIndex:row] intValue]; label.text = [NSString stringWithFormat:@"%d", weight]; label.textAlignment = UITextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:30]; [label autorelease]; return label; } else { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)]; label.text = [pickerArrayHalf objectAtIndex:row]; label.textAlignment = UITextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:30]; [label autorelease]; return label; } } else { int reps = [[pickerArray objectAtIndex:row] intValue]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)]; label.text = [NSString stringWithFormat:@"%d reps", reps]; label.textAlignment = UITextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:30]; [label autorelease]; return label; } } </code></pre> <p>Update again:</p> <p>I added this code:</p> <pre><code>- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"Selected Color: %@. Index of selected row: %i", [pickerArray objectAtIndex:row], row); NSLog(@"Component 0: %@", [repPicker selectedRowInComponent:0]); NSLog(@"Component 1: %@", [repPicker selectedRowInComponent:1]); } </code></pre> <p>I now get a crash when scrolling the clicker now at line: NSLog(@"Component 1: %@", [repPicker selectedRowInComponent:1]);</p> <p>error:</p> <blockquote> <p><strong>* Terminating app due to uncaught exception 'NSRangeException', reason: '*</strong> -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'</p> </blockquote> <p>I also got <code>Component 0: (null)</code></p> <p>Update:</p> <p>Code for declaring pickerArray:</p> <pre><code>pickerArray = [[NSMutableArray alloc] initWithCapacity:700]; for ( int i = 0 ; i &lt;= 1000 ; ++i) [pickerArray addObject:[NSString stringWithFormat:@"%d", i]]; pickerArrayHalf = [[NSMutableArray alloc]initWithCapacity:2]; [pickerArrayHalf addObject:@"0 lb"]; [pickerArrayHalf addObject:@"1/2 lb"]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload