How to use UIPickerView?

This post deals with using UIPickerView in an application

 

Lets say the ViewController class in which UIPickerView is to be populated is myViewController

Open myViewController.xib

Drag UIPickerView from Library and add it to the view as shown below

iPhone Application Tutorials

Now come to myViewController.h file

Add the following lines of code highlighted

Now move to myViewController.m

Add the following code

@synthesize picker;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return @"john";
}

The above delegate methods take care of following conditions

In the above code we have assumed that number of components is 1 and there are 3 rows in that component

We want to populate the rows with “john”

Save all this

Now it’s time to make connections, so come again to myViewController.xib

Make connections as shown below

iphone Application Tutorials

Save and execute and the output is shown below

iPhone Application Tutorials