[英]How To Get Selected Value From UIPickerView With 2 Components
我在这里有一个问题,我问如何从UIPickerView获取值 ( 如何从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"];
冷冻? 穿一件毛衣:-)其实,日志上写着什么-您知道它在哪里冻结吗? 放置NSLog语句以查找确切的行,或通过调试器逐步执行。
也许是NSLog(@"repPicker: %@", repPicker);
可以帮助弄清楚那里发生了什么。
编辑:您正在初始化具有容量700的pickerArray并添加1000个对象。 这是行不通的。 我会用pickerArray = [[NSMutableArray] alloc] init];
并添加1000个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.