当我使用UIPickerView加载视图时出现以下错误:
* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSCFNumber isEqualToString:]:无法识别的选择器发送到实例0x5906000'
以下是与选择器相关的所有代码:
-(void)viewDidLoad:
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 1 ; i <= 100 ; i ++ )
[array addObject:[NSNumber numberWithInt:i]];
weightArray = array;
//[array release];
#pragma mark - Weight Picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [weightArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [weightArray objectAtIndex:row];
}
- (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 (thePickerView.tag==1)
{
//float weight = [[pickerArray objectAtIndex:row] intValue];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
float weight = [[pickerArray objectAtIndex:row] floatValue];
label.text = [NSString stringWithFormat:@"%f lb", weight];
//label.text = [NSString stringWithFormat:@"%f lb", weight];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize: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 systemFontOfSize:30];
[label autorelease];
return label;
}
}
答案 0 :(得分:5)
费萨尔 我认为问题在于
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [weightArray objectAtIndex:row];
}
在上面的方法中... weightArray是NSNumber的数组。并且你将它视为字符串,你应该尝试遵循
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
int weight = [[weightArray objectAtIndex:row] intValue];
NSString *pickerTitle = [NSString stringWithFormat:@"%d",weight];
return pickerTitle;
}
关于选择器方法
#pragma mark - Weight Picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
use like this...
if(thePickerView.tag==1)
{
return 1;
}
else if(thePickerView.tag==2)
{
return 2; //just for example...
}
else
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [weightArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(thePickerView.tag==1)
{
return [weightArray objectAtIndex:row];
}
else if(thePickerView.tag==1)
{
return ;//your other value and so on.....
}
}
谢谢,
答案 1 :(得分:0)
这意味着您正在尝试将值与不存在的方法进行比较。换句话说,NSNumber没有方法isEqualToString实现。对于数字使用此方法:
if(someNumber == [someString intValue]){
}