Firstly, you must have noticed that a keyboard appears when you click on any TextField/TextView. That keyboard is just a default Input View of Textfield or TextView.

We can change it with the help of UI Kit Framework.

Now lets get started,

Write/copy the following code in ViewController.h.

  1. #
  2. import < UIKit / UIKit.h >
  3. @interface ViewController: UIViewController
  4. {
  5. UIDatePicker * DatePicker;
  6. UIPickerView * Picker;
  7. NSArray * CountryArray;
  8. NSArray * heightInch;
  9. NSArray * heightFeet;
  10. }
  11. @property(strong, nonatomic) IBOutlet UITextField * dateSelectionTextField;
  12. @property(strong, nonatomic) IBOutlet UITextField * countryTextField;
  13. @property(strong, nonatomic) IBOutlet UITextField * heightTextField;
  14. @end

Copy all in same sequence in ViewController.m

  1. #
  2. import "ViewController.h"
  3. @interface ViewController() < UIPickerViewDataSource, UIPickerViewDelegate >
  4. @end
  5. @implementation ViewController
  6. @synthesize dateSelectionTextField, countryTextField, heightTextField;
  7. - (void) viewDidLoad
  8. {
  9. [super viewDidLoad];
  10. // Do any additional setup after loading the view, typically from a nib.

Now firstly we are going to allocate DatePicker and set input of dateSelectionTextField to DatePicker.

  1. //For DatePicker
  2. DatePicker =
  3. [
  4. [UIDatePicker alloc] init
  5. ];
  6. DatePicker.datePickerMode = UIDatePickerModeDate; //You can change Picker mode here
  7. [self.dateSelectionTextField setInputView: DatePicker];

Same for Picker but with some custom colour changes.

  1. //For Picker
  2. Picker =
  3. [
  4. [UIPickerView alloc] init
  5. ];
  6. Picker.delegate = self;
  7. Picker.dataSource = self;
  8. [Picker setShowsSelectionIndicator: YES];
  9. Picker.tintColor = [UIColor blackColor];
  10. Picker.backgroundColor = [UIColor colorWithRed: 153.0 green: 204.0 blue: 255.0 alpha: 2];

These Arrays holds the values of content in Picker.

  1. //Arrays
  2. CountryArray =
  3. [
  4. [NSArray alloc] initWithObjects: @ "USA", @ "India", @ "France", nil
  5. ];
  6. heightFeet =
  7. [
  8. [NSArray alloc] initWithObjects: @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", nil
  9. ];
  10. heightInch =
  11. [
  12. [NSArray alloc] initWithObjects: @ "0", @ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "10", @ "11", nil
  13. ];

This is to set InputView of TextField.

Note: One Picker allocation can be used for multiple text fields.

  1. //ForPicker
  2. [self.countryTextField setInputView: Picker];
  3. [self.heightTextField setInputView: Picker];

Now will make a Toolbar which will provide ease to user to go to another textfields.

  1. //For ToolBar
  2. UIToolbar * toolBar =
  3. [
  4. [UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 45)
  5. ];
  6. [toolBar setTintColor: [UIColor blueColor]];
  7. UIBarButtonItem * doneBtn =
  8. [
  9. [UIBarButtonItem alloc] initWithTitle: @ "Done"
  10. style: UIBarButtonItemStyleDone target: self action: @selector(Done)
  11. ];
  12. UIBarButtonItem * space =
  13. [
  14. [UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil
  15. ];
  16. UIBarButtonItem * prevBtn =
  17. [
  18. [UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @ "765-arrow-left.png"] style: UIBarButtonItemStylePlain target: self action: @selector(previous)
  19. ];
  20. UIBarButtonItem * nextBtn = [
  21. [UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @ "766-arrow-right.png"] style: UIBarButtonItemStylePlain target: self action: @selector(next)
  22. ];
  23. prevBtn.width = 20.0;
  24. nextBtn.width = 70.0;
  25. [toolBar setItems: [NSArray arrayWithObjects: prevBtn, nextBtn, space, doneBtn, nil]];

Now this will add the toolbar to textfields.

  1. //for date textfield
  2. [self.dateSelectionTextField setInputAccessoryView: toolBar];
  3. //for country textfield
  4. [self.countryTextField setInputAccessoryView: toolBar];
  5. //for Height textfield
  6. [self.heightTextField setInputAccessoryView: toolBar];
  7. }

This gives Action to Done button on toolbar.

  1. //For Date picker
  2. -(void) Done
  3. {
  4. //for date picker only
  5. NSDateFormatter * formatter =
  6. [
  7. [NSDateFormatter alloc] init
  8. ];
  9. [formatter setDateFormat: @ "dd/MMM/YYYY"];
  10. self.dateSelectionTextField.text = [NSString stringWithFormat: @ "%@", [formatter stringFromDate: DatePicker.date]];
  11. [self.dateSelectionTextField resignFirstResponder];
  12. //forPicker
  13. [self.countryTextField resignFirstResponder];
  14. [self.heightTextField resignFirstResponder];
  15. }

Now these are the required method for PickerView.

  1. //For Picker
  2. -(NSInteger) numberOfComponentsInPickerView: (UIPickerView * ) pickerView
  3. {
  4. if ([countryTextField isFirstResponder])
  5. {
  6. return 1;
  7. } else if ([heightTextField isFirstResponder])
  8. {
  9. return 2;
  10. } else
  11. return 1;
  12. }
  13. - (NSInteger) pickerView: (UIPickerView * ) pickerView numberOfRowsInComponent: (NSInteger) component
  14. {
  15. if ([countryTextField isFirstResponder])
  16. {
  17. return [CountryArray count];
  18. } else if ([heightTextField isFirstResponder])
  19. {
  20. if (component == 0)
  21. {
  22. return [heightFeet count];
  23. } else {
  24. return [heightInch count];
  25. }
  26. }
  27. return 1;
  28. }
  29. - (NSString * ) pickerView: (UIPickerView * ) pickerView titleForRow: (NSInteger) row forComponent: (NSInteger) component {
  30. if ([countryTextField isFirstResponder])
  31. {
  32. return [CountryArray objectAtIndex: row];
  33. } else if ([heightTextField isFirstResponder])
  34. {
  35. if (component == 0)
  36. {
  37. return [heightFeet objectAtIndex: row];
  38. } else
  39. {
  40. return [heightInch objectAtIndex: row];
  41. }
  42. }
  43. return 0;
  44. }
  45. - (void) pickerView: (UIPickerView * ) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component
  46. {
  47. if ([countryTextField isFirstResponder])
  48. {
  49. self.countryTextField.text = [CountryArray objectAtIndex: row];
  50. } else if ([heightTextField isFirstResponder])
  51. {
  52. [heightTextField setText: [NSString stringWithFormat: @ "%@' %@''", [heightFeet objectAtIndex: [Picker selectedRowInComponent: 0]],
  53. [heightInch objectAtIndex: [Picker selectedRowInComponent: 1]]
  54. ]];
  55. }
  56. }

You may have a question that why these if-else statements are used?.

So the answer is, they are providing different values and different Textfields via Same Picker.

Now lets go ahead, the following coding is providing action to previous and next buttons on toolbar.

  1. -(void) previous {
  2. if ([countryTextField isFirstResponder])
  3. {
  4. [self.countryTextField resignFirstResponder];
  5. [dateSelectionTextField becomeFirstResponder];
  6. } else if ([heightTextField isFirstResponder])
  7. {
  8. [self.heightTextField resignFirstResponder];
  9. [countryTextField becomeFirstResponder];
  10. }
  11. }
  12. - (void) next
  13. {
  14. if ([dateSelectionTextField isFirstResponder])
  15. {
  16. NSDateFormatter * formatter =
  17. [
  18. [NSDateFormatter alloc] init
  19. ];
  20. [formatter setDateFormat: @ "dd/MMM/YYYY"];
  21. self.dateSelectionTextField.text = [NSString stringWithFormat: @ "%@", [formatter stringFromDate: DatePicker.date]];
  22. [self.dateSelectionTextField resignFirstResponder];
  23. [countryTextField becomeFirstResponder];
  24. } else if ([countryTextField isFirstResponder])
  25. {
  26. [self.countryTextField resignFirstResponder];
  27. [heightTextField becomeFirstResponder];
  28. }
  29. }

This is an important method which prevents the values of different textfields from mixing up.

  1. -(void) textFieldDidBeginEditing: (UITextField * ) textField
  2. {
  3. [Picker reloadAllComponents];
  4. }
  5. - (void) didReceiveMemoryWarning
  6. {
  7. [super didReceiveMemoryWarning];
  8. // Dispose of any resources that can be recreated.
  9. }
  10. @end

ALL DONE IN CODING PART

Open Main.storyboard

  1. Drag a TextField in ViewController and resize it.
  2. You can change its Alignment to Centre.
  3. Set new Referencing Outlet named “dateSelectionTextField” and delegate.
  4. In same way add two more textfields.
  5. Set Properties and delegates there.

Now take a deep breath and run your Project.

Your storyboard will look like the following screenshot,

Storyboard

Date Formats

@"dd/mm/yyyy" for 20/08/2015.
@"dd/MMM/YYYY" for 22/Nov/2015.
@"dd/MMM/YYYY hh:min a” for 19/Nov/2015 03:10