我在这里有一个问题我在哪里询问如何获取UIPickerView(How To Get Selected Value From UIPickerView)的值。我得到了一个带有1个组件的选择器的解决方案,但我现在试图从带有2个组件的选择器中获取值。
此代码适用于包含1个组件的选择器:
NSInteger row;
NSString *weightSelected;
row = [repPicker selectedRowInComponent:0];
weightSelected = [pickerArray objectAtIndex:row];
我为这个带有2个组件的选择器累了这个代码,但它很冷。控制台中也没有错误。冻结的线是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];
更新:
#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;
}
}
再次更新:
我添加了这段代码:
- (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]);
}
现在滚动点击器时,我现在遇到崩溃:NSLog(@“Component 1:%@”,[repPicker selectedRowInComponent:1]);
错误:
*由于未捕获的异常'NSRangeException'而终止应用,原因: '* - [NSMutableArray objectAtIndex:]: 索引1超出界限[0 .. 0]'
我也得到Component 0: (null)
更新
声明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"];
答案 0 :(得分:0)
冻结?穿毛衣:-)实际上,日志说什么 - 你知道它在哪里冻结吗?放置NSLog语句以找到确切的行,或者使用调试器逐步执行。
答案 1 :(得分:0)
也许NSLog(@"repPicker: %@", repPicker);
可以帮助弄清楚那里发生了什么。
编辑:
您正在初始化容量为700的pickerArray并添加1000个对象。这不起作用。我会使用pickerArray = [[NSMutableArray] alloc] init];
并添加1000个对象。