-1

My numberOfComponentsInPickerView :

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

My 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;
}

My titleForRow like this:

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

my didSelectRow like this after edited like Paras Joshi's said :

- (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];
}

it still gives me error " bad receiver type 'int' " and i still don't get it how to fix it. how my label get data from titleForRow?

both input for year month and day all are number only (from 0 to 99) so that i wrote -> return [NSString stringWithFormat:@"%d", row];

i dont put data for my pickerview at - (void)viewDidLoad because i want my labelDate1 or labelDate2 got data from pickerview. is there any possible my label get data from pickerview like i wrote above? or must i write my data at - (void)viewDidLoad ?

for any help, thank you for watching my question.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
Piyo
  • 181
  • 1
  • 2
  • 9
  • dude please use %d insted of %@ here this problem occure just write this format [year stringByAppendingFormat:@" : %d : %d", month, day] – Paras Joshi Jun 23 '12 at 10:23
  • and also get int year; int month;,etc..... – Paras Joshi Jun 23 '12 at 10:32
  • i already used it before like you said but it still give me error "bad receiver type 'int'" point to year, both at the labelDate1 and labelDate2. – Piyo Jun 23 '12 at 10:44
  • you try to get it with NSInteger and also possible then use datepicker and get year,month and day with components its very easy.... – Paras Joshi Jun 23 '12 at 10:46
  • did you want to date pick from uipicker? – Dinesh Jun 23 '12 at 11:11
  • [picker selectedRowInComponent:0] returns the index of selected row which is NSInteger. You are trying to assign it to year which is NSString. – BumbleBoks Jun 23 '12 at 11:14
  • yes but both date (year, month and day) pick from uipicker only 2 digit number from 00 to 99. i dont know how to use datepicker, implement it into my pickerview. so i made it manually. – Piyo Jun 23 '12 at 11:15
  • labelDate1.text = [year stringByAppendingFormat:@" : %d : %d", month, day]; this is not right ... year is integer you can not use string by appending mthod with int – TheTiger Jun 23 '12 at 11:53
  • write like this - NSString *myDate = [[[NSString stringWithFormat:@"%d/",year]stringByAppendingFormat:@"%d/",month]stringByAppendingFormat:@"%d",day]; then add this string to your label.text – TheTiger Jun 23 '12 at 11:56

3 Answers3

0

Did you set the UIPickerView's delegate?I mean, did your pickerView:didSelectRow:inComponent get called?

CarmeloS
  • 7,868
  • 8
  • 56
  • 103
0

Hello @Piyo Piyo your

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

method is totally wrong, you are assigning NSInteger to NSString. Thats why you are getting en error. You should use NSArray for putting data in picker view row and picking data from here. Here i'm going to make a small sample code.

This is your viewController.h file...

#import <UIKit/UIKit.h>

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

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

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

@end

there are three array which will contain the data for day, month and year respectively and three UILabel for showing the data. and obviously one pickerview.

Now come to on your viewController.m part.

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

I hope this tutorial will help you. Thank you!

TheTiger
  • 13,264
  • 3
  • 57
  • 82
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];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200