我的标签无法从pickerview获得价值

时间:2012-06-23 10:18:58

标签: iphone ios5 xcode4.3

我的numberOfComponentsInPickerView:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

我的numberOfRowsInComponent:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
        return 100;
    if (component == 1)
        return 100;
    if (component == 2)
        return 100;

    return 0;
}

我的titleForRow是这样的:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{    
    if (component == 0)
        return [NSString stringWithFormat:@"%d", row];
    if (component == 1)
        return [NSString stringWithFormat:@"%d", row];
    if (component == 2)
        return [NSString stringWithFormat:@"%d", row];

    return 0;
}

我的didSelectRow是这样的 经过编辑后像Paras Joshi所说:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    int year = [picker selectedRowInComponent:0];
    int month = [picker selectedRowInComponent:1];
    int day = [picker selectedRowInComponent:2];
    if(viewPicker.tag == 1)
        labelDate1.text = [year stringByAppendingFormat:@" : %d : %d", month, day];
    else
        labelDate2.text = [year stringByAppendingFormat:@" : %d : %d", month, day];
}

它仍然给我错误“坏接收器类型'int'”,我仍然不知道如何解决它。我的标签如何从titleForRow获取数据?

年月和日的输入都只是数字(从0到99)所以我写了 - > return [NSString stringWithFormat:@“%d”,row];

我没有把我的pickerview的数据放在 - (void)viewDidLoad ,因为我希望我的labelDate1或labelDate2从pickerview获取数据。有没有可能我的标签从像上面写的pickerview获取数据?或者我必须在 - (void)viewDidLoad

中写下我的数据

任何帮助,谢谢你看我的问题。

3 个答案:

答案 0 :(得分:0)

你是否设置了UIPickerView的委托?我的意思是,你的pickerView:didSelectRow:inComponent被调用吗?

答案 1 :(得分:0)

你好@Piyo Piyo你的

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

方法完全错误,您将NSInteger分配给NSString。这就是为什么你得到错误。您应该使用NSArray将数据放入选择器视图行并从此处选择数据。在这里,我将制作一个小样本代码。

这是你的viewController.h文件......

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIPickerView *myPickerView;

    NSMutableArray *arrayYear;
    NSMutableArray *arrayMonth;
    NSMutableArray *arrayDay;

    UILabel *lblDay;
    UILabel *lblMonth;
    UILabel *lblYear;
}

@end

有三个数组,分别包含日,月和年的数据,以及三个用于显示数据的UILabel。显然是一个挑选者。

现在来看看你的viewController.m部分。

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

   // UIPickerView declaration by coding 
    myPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 216.0)];
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];

    // add some day number to day array
    arrayDay = [[NSMutableArray alloc]init];
    for(int i=1;i<6;i++)
    {
        NSString *string = [NSString stringWithFormat:@"%d",i];
        [arrayDay addObject:string];
    }

    // add some month string to month array
    arrayMonth = [[NSMutableArray alloc]init];
    [arrayMonth addObject:@"June"];
    [arrayMonth addObject:@"July"];
    [arrayMonth addObject:@"August"];
    [arrayMonth addObject:@"September"];
    [arrayMonth addObject:@"October"];

    // add some year number to year array
    arrayYear = [[NSMutableArray alloc]init];
    for(int i=2006;i<2011;i++)
    {
        NSString *string = [NSString stringWithFormat:@"%d",i];
        [arrayYear addObject:string];
    }

     //set initially text to day label
     lblDay = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 250.0, 90.0, 50.0)];
    [lblDay setText:[arrayDay objectAtIndex:0]];
    [self.view addSubview:lblDay];

    //set initially text to month label
    lblMonth = [[UILabel alloc]initWithFrame:CGRectMake(110.0, 250.0, 90.0, 50.0)];
    [lblMonth setText:[arrayMonth objectAtIndex:0]];
    [self.view addSubview:lblMonth];

    //set initially text to year label
    lblYear = [[UILabel alloc]initWithFrame:CGRectMake(210.0, 250.0, 90.0, 50.0)];
    [lblYear setText:[arrayYear objectAtIndex:0]];
    [self.view addSubview:lblYear];


    //set initially selection to each component of UIPickerView
    [myPickerView selectRow:1 inComponent:0 animated:NO];
    [myPickerView selectRow:1 inComponent:1 animated:NO];
    [myPickerView selectRow:1 inComponent:2 animated:NO];
}

//here you are returning the number of component, which are 3 in this case
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 3;
}

//this is your didSelectRow method and here you are assigning a new text to 
//label accordingly to if condition
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    if(component == 0)
    {
        lblDay.text = [arrayDay objectAtIndex:row];
    }
    if(component == 1)
    {
        lblMonth.text = [arrayMonth objectAtIndex:row];
    }

    if(component == 2)
    {
        lblYear.text = [arrayYear objectAtIndex:row];
    }

}


 //this is your numberOf row in component in this case each component have 5 
 //rows thats why i wrote only return 5; here you can put if condition here for 
 //returning different rows for each component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
     return 5;
}


 // and this is showing title on rows
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if (component == 0)
    return [arrayDay objectAtIndex:row];
    else if (component == 1)
    return [arrayMonth objectAtIndex:row];
    else if (component == 2)
    return [arrayYear objectAtIndex:row];
    else
        return 0;
}

我希望本教程能为您提供帮助。谢谢!

答案 2 :(得分:0)

在你的程序中,你将字符串附加到一个问题的整数。 这段代码就是这个问题。因为year是一个整数。

labelDate1.text = [year stringByAppendingFormat:@" : %d : %d", month, day];

请查看此代码。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    int year = [picker selectedRowInComponent:0];
    int month = [picker selectedRowInComponent:1];
    int day = [picker selectedRowInComponent:2];
    NSString *date = @"";
    if(viewPicker.tag == 1)
        labelDate1.text = [date stringByAppendingFormat:@" %d : %d : %d",year, month, day];
    else
        labelDate2.text = [date stringByAppendingFormat:@"%d : %d : %d", year, month, day];
}