A picker view lets the user choose between certain options by spinning a wheel on the screen. Picker views are well suited for choosing things like dates and times (as the date picker does) that have a moderate number of discrete options. Other examples include picking which armor to wear in a game and picking a font for text in a word processor. The list of options should be ordered in some logical way to make scanning easier Apple Documentation

Steps:

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name ( Lets say SimplePickerView).
  3. Click next button and the Project will be created.
  4. Now go to Main StoryBoard of the Project and In the Object Library, select the “UiTextField , and a button” object and drag it into the view. Put Button on top of UITextField and make the Proper IBOutlet connection and Action for Button. Next take a scroll View  and put it into the view. On top of Scroll View take a toolbar and put two toolbar button item . make proper connection in “ViewController.m “. Connect delegate and dataSource of the pickerView to ViewControoler.

After Connectimg the IBOutlet and writing the delegates Your View Controller will Look something Like this

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

#pragma mark- Action Methods

– (IBAction)buttonAction:(id)sender {

}

– (IBAction)cancelButtonAction:(id)sender {

}

– (IBAction)doneButtonAction:(id)sender {

}

5. Now declare an array and initialize it in ViewDidLoad . Keep initially pickerView and toolbar as hidden(I have kept it, You can do according to your requirement)

@implementation ViewController{

NSArray *pickerArray;

}

– (void)viewDidLoad {

[super viewDidLoad];

pickerArray = [[NSArray alloc]initWithObjects:@”Mango”,

@”Apple,@”Guava”,@”Orange”,@”Grapes”, nil];

self.pickerView.hidden = YES;

self.toolBar.hidden = YES;

// Do any additional setup after loading the view, typically from a nib.

}

6. Now you will have to write the dataSource and delegate methods of the ScrollView.

#pragma mark – Picker View Data source

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

return 1;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView

numberOfRowsInComponent:(NSInteger)component{

return [pickerArray count];

}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:

(NSInteger)row inComponent:(NSInteger)component{

[self.textField setText:[pickerArray objectAtIndex:row]];

}

– (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:

(NSInteger)row forComponent:(NSInteger)component{

return [pickerArray objectAtIndex:row];

}

 

7. Now just define the action methods for Your Button.

#pragma mark- Action Methods

– (IBAction)buttonAction:(id)sender {

self.pickerView.hidden = NO;

self.toolBar.hidden = NO;

}

– (IBAction)cancelButtonAction:(id)sender {

self.pickerView.hidden = YES;

self.toolBar.hidden = YES;

self.textField.text=@””;

}

– (IBAction)doneButtonAction:(id)sender {

self.pickerView.hidden = YES;

self.toolBar.hidden = YES;

}

8. Run the code and see the desired output. Enjoy Coding 🙂

Advertisements