-1

我的 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吗?

寻求帮助,感谢您观看我的问题。

4

3 回答 3

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 用于显示数据。显然是一个pickerview。

现在来看看你的 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;
}

我希望本教程对您有所帮助。谢谢!

于 2012-06-23T11:40:50.800 回答
0

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

于 2012-06-23T10:29:45.773 回答
0

In your program you are appending string to an integer that is the issue. This code makes the issue. Because year is an integer.

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

Please check with this code.

- (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];
}
于 2012-06-23T11:44:32.750 回答