Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add 1-100 numbers to picker view programmatically?

I created a UIPickerView manually with three components. I want to display numbers from 1 to 100 in component1. How can I do it programmatically?

like image 693
venkateswararao burle Avatar asked Dec 04 '25 19:12

venkateswararao burle


2 Answers

I guess you have implemented the methods

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

and

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{

    if (component == 1 )
    {
        return [NSString stringWithFormat:@"%d",(row+1)];
    }

    return @"";//required elements
}
like image 153
Ilanchezhian Avatar answered Dec 06 '25 07:12

Ilanchezhian


in .h file

NSMutableArray *data ;

--

data = [NSMutableArray alloc] init];
for (int i=1; i<=100; i++){
     [data addObject:[NSString stringWithFormat:@"%d",i];
}

// this will add 1-100 values in your array (data) Then Implement UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

return [data count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [data objectAtIndex:row];
}
like image 29
aahsanali Avatar answered Dec 06 '25 09:12

aahsanali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!