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,
- Open Xcode.
- Click on Create a new Xcode project.
- Now select Single View Application.
- Click -> Next.
- Enter -> Product Name and Other blank fields (if any).
- Now select the location where you want to save your project and click on Create.
Write/copy the following code in ViewController.h.
- #
- import < UIKit / UIKit.h >
-
- @interface ViewController: UIViewController
-
- {
- UIDatePicker * DatePicker;
- UIPickerView * Picker;
- NSArray * CountryArray;
- NSArray * heightInch;
- NSArray * heightFeet;
- }
-
- @property(strong, nonatomic) IBOutlet UITextField * dateSelectionTextField;
- @property(strong, nonatomic) IBOutlet UITextField * countryTextField;
- @property(strong, nonatomic) IBOutlet UITextField * heightTextField;
-
-
- @end
Copy all in same sequence in ViewController.m
- #
- import "ViewController.h"
-
- @interface ViewController() < UIPickerViewDataSource, UIPickerViewDelegate >
-
- @end
-
- @implementation ViewController
- @synthesize dateSelectionTextField, countryTextField, heightTextField;
-
-
- - (void) viewDidLoad
- {
- [super viewDidLoad];
-
Now firstly we are going to allocate DatePicker and set input of dateSelectionTextField to DatePicker.
-
- DatePicker =
- [
- [UIDatePicker alloc] init
- ];
- DatePicker.datePickerMode = UIDatePickerModeDate;
- [self.dateSelectionTextField setInputView: DatePicker];
Same for Picker but with some custom colour changes.
-
- Picker =
- [
- [UIPickerView alloc] init
- ];
- Picker.delegate = self;
- Picker.dataSource = self;
- [Picker setShowsSelectionIndicator: YES];
- Picker.tintColor = [UIColor blackColor];
- Picker.backgroundColor = [UIColor colorWithRed: 153.0 green: 204.0 blue: 255.0 alpha: 2];
These Arrays holds the values of content in Picker.
-
- CountryArray =
- [
- [NSArray alloc] initWithObjects: @ "USA", @ "India", @ "France", nil
- ];
- heightFeet =
- [
- [NSArray alloc] initWithObjects: @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", nil
- ];
- heightInch =
- [
- [NSArray alloc] initWithObjects: @ "0", @ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "10", @ "11", nil
- ];
This is to set InputView of TextField.
Note: One Picker allocation can be used for multiple text fields.
-
- [self.countryTextField setInputView: Picker];
- [self.heightTextField setInputView: Picker];
Now will make a Toolbar which will provide ease to user to go to another textfields.
-
- UIToolbar * toolBar =
- [
- [UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 45)
- ];
- [toolBar setTintColor: [UIColor blueColor]];
- UIBarButtonItem * doneBtn =
- [
- [UIBarButtonItem alloc] initWithTitle: @ "Done"
- style: UIBarButtonItemStyleDone target: self action: @selector(Done)
- ];
- UIBarButtonItem * space =
- [
- [UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil
- ];
- UIBarButtonItem * prevBtn =
- [
- [UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @ "765-arrow-left.png"] style: UIBarButtonItemStylePlain target: self action: @selector(previous)
- ];
- UIBarButtonItem * nextBtn = [
- [UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @ "766-arrow-right.png"] style: UIBarButtonItemStylePlain target: self action: @selector(next)
- ];
-
- prevBtn.width = 20.0;
- nextBtn.width = 70.0;
-
- [toolBar setItems: [NSArray arrayWithObjects: prevBtn, nextBtn, space, doneBtn, nil]];
Now this will add the toolbar to textfields.
-
- [self.dateSelectionTextField setInputAccessoryView: toolBar];
-
-
- [self.countryTextField setInputAccessoryView: toolBar];
-
-
- [self.heightTextField setInputAccessoryView: toolBar];
- }
This gives Action to Done button on toolbar.
-
- -(void) Done
- {
-
- NSDateFormatter * formatter =
- [
- [NSDateFormatter alloc] init
- ];
- [formatter setDateFormat: @ "dd/MMM/YYYY"];
- self.dateSelectionTextField.text = [NSString stringWithFormat: @ "%@", [formatter stringFromDate: DatePicker.date]];
- [self.dateSelectionTextField resignFirstResponder];
-
-
- [self.countryTextField resignFirstResponder];
- [self.heightTextField resignFirstResponder];
- }
Now these are the required method for PickerView.
-
- -(NSInteger) numberOfComponentsInPickerView: (UIPickerView * ) pickerView
- {
- if ([countryTextField isFirstResponder])
- {
-
- return 1;
- } else if ([heightTextField isFirstResponder])
- {
-
- return 2;
- } else
- return 1;
- }
-
- - (NSInteger) pickerView: (UIPickerView * ) pickerView numberOfRowsInComponent: (NSInteger) component
- {
- if ([countryTextField isFirstResponder])
- {
-
- return [CountryArray count];
-
- } else if ([heightTextField isFirstResponder])
- {
-
- if (component == 0)
- {
- return [heightFeet count];
- } else {
- return [heightInch count];
- }
- }
- return 1;
- }
-
- - (NSString * ) pickerView: (UIPickerView * ) pickerView titleForRow: (NSInteger) row forComponent: (NSInteger) component {
- if ([countryTextField isFirstResponder])
- {
- return [CountryArray objectAtIndex: row];
-
- } else if ([heightTextField isFirstResponder])
- {
- if (component == 0)
- {
-
- return [heightFeet objectAtIndex: row];
- } else
- {
- return [heightInch objectAtIndex: row];
- }
- }
-
- return 0;
- }
-
- - (void) pickerView: (UIPickerView * ) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component
- {
-
- if ([countryTextField isFirstResponder])
- {
- self.countryTextField.text = [CountryArray objectAtIndex: row];
- } else if ([heightTextField isFirstResponder])
- {
-
- [heightTextField setText: [NSString stringWithFormat: @ "%@' %@''", [heightFeet objectAtIndex: [Picker selectedRowInComponent: 0]],
- [heightInch objectAtIndex: [Picker selectedRowInComponent: 1]]
- ]];
- }
-
- }
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.
- -(void) previous {
- if ([countryTextField isFirstResponder])
- {
-
- [self.countryTextField resignFirstResponder];
- [dateSelectionTextField becomeFirstResponder];
- } else if ([heightTextField isFirstResponder])
- {
-
- [self.heightTextField resignFirstResponder];
- [countryTextField becomeFirstResponder];
- }
-
- }
-
- - (void) next
- {
- if ([dateSelectionTextField isFirstResponder])
- {
- NSDateFormatter * formatter =
- [
- [NSDateFormatter alloc] init
- ];
- [formatter setDateFormat: @ "dd/MMM/YYYY"];
- self.dateSelectionTextField.text = [NSString stringWithFormat: @ "%@", [formatter stringFromDate: DatePicker.date]];
-
- [self.dateSelectionTextField resignFirstResponder];
- [countryTextField becomeFirstResponder];
-
- } else if ([countryTextField isFirstResponder])
- {
- [self.countryTextField resignFirstResponder];
- [heightTextField becomeFirstResponder];
-
- }
- }
This is an important method which prevents the values of different textfields from mixing up.
- -(void) textFieldDidBeginEditing: (UITextField * ) textField
- {
- [Picker reloadAllComponents];
- }
-
- - (void) didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
-
- }
- @end
ALL DONE IN CODING PART
Open Main.storyboard
- Drag a TextField in ViewController and resize it.
- You can change its Alignment to Centre.
- Set new Referencing Outlet named “dateSelectionTextField” and delegate.
- In same way add two more textfields.
- Set Properties and delegates there.
Now take a deep breath and run your Project.
Your storyboard will look like the following screenshot,
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