I have a question here where I asked how to get a value form UIPickerView ( How To Get Selected Value From UIPickerView ). I got a solution for a picker with 1 component but I'm trying to get the value from picker with 2 component now.
This code works for picker with 1 component:
NSInteger row;
NSString *weightSelected;
row = [repPicker selectedRowInComponent:0];
weightSelected = [pickerArray objectAtIndex:row];
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 row2 = [repPicker selectedRowInComponent:1];
:
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];
UPDATE:
#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;
}
}
Update again:
I added this 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]);
}
I now get a crash when scrolling the clicker now at line: NSLog(@"Component 1: %@", [repPicker selectedRowInComponent:1]);
error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
I also got Component 0: (null)
Update:
Code for declaring pickerArray:
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 0 ; i <= 1000 ; ++i)
[pickerArray addObject:[NSString stringWithFormat:@"%d", i]];
pickerArrayHalf = [[NSMutableArray alloc]initWithCapacity:2];
[pickerArrayHalf addObject:@"0 lb"];
[pickerArrayHalf addObject:@"1/2 lb"];
Freezing? Wear a sweater :-) Actually, what does the log say - do you know where it's freezing? Put NSLog statements to find the exact line, or step through with the debugger.
Maybe a NSLog(@"repPicker: %@", repPicker);
could help to figure out what is going on there.
Edit: You are initializing the pickerArray with capacity 700 and add 1000 objects. This does not work. I would use pickerArray = [[NSMutableArray] alloc] init];
and add the 1000 objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.