iphone - 为什么我的 UIPickerView 崩溃了?

标签 iphone objective-c

当我使用 UIPickerView 加载 View 时出现以下错误:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 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;
    }
}

最佳答案

费萨尔, 我觉得问题出在

- (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.....
        }
    }

谢谢,

关于iphone - 为什么我的 UIPickerView 崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713225/

相关文章:

c++ - 如何在其他文件中使用 .mm 文件中的 C++ 类对象?

javascript - 如何退出全屏 Web 应用程序

iphone - UIDatePickerModeCountDownTimer 模式下的 UIDatePicker : how to get/set the hours and minutes via code?

ios - 为什么不调用 UIViewControllerTransitioningDelegate 的 presentationControllerForPresentedViewController 方法?

objective-c - 可编写脚本的应用程序示例

iphone - Xcode 3.2.6 的 iOS 教程

iphone - 为所有 navigationcontroller 和 navigationitem 设置 titleview

javascript - 有条件地为 iPhone 4 设置视口(viewport)?

ios - NSFetchedResultsController-controllerDidChangeContent : - Attempt to create two animations for cell with userInfo

objective-c - 如何将 NSInteger 转换为二进制(字符串)值