Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone datepicker instead of keyboard?

How would you do that?

I have a form with a UITextField. When I click in it I would like to display a UIDatePicker instead of the default Keyboard that pops-up by default? Note that there are also other elements on my form.

like image 324
xpepermint Avatar asked Jan 23 '11 23:01

xpepermint


People also ask

Is there a way to disable typing pasting into the date input and only allowing users to select from the date picker?

For antd date picker, you need to set dispaly: none in ant-calendar-input-wrap class. Save this answer.

Can I use the new iOS 14 style for the date time picker?

The Date Picker What's new in date picker though is that it comes with two new, additional display options: compact and inline. Both of them can display both date and time, or just either date or time.

What is DatePicker in Swift?

DatePicker is a control used in iOS applications to get the date and time values from the user. We can allow the user to either enter the time in point or time interval. class UIDatePicker : UIControl.


2 Answers

I think this is a more official way to do this:

UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease]; datePicker.datePickerMode = UIDatePickerModeDate; [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; datePicker.tag = indexPath.row; textField.inputView = datePicker; 
like image 190
Johan Kool Avatar answered Sep 23 '22 05:09

Johan Kool


in .h

UIDatePicker *datePicker; NSDate *date; UIToolbar *keyboardToolbar; 

in ViewDidLoad:

if (keyboardToolbar == nil) {     keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];     [keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];      UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];      UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:@"Accept" style:UIBarButtonItemStyleDone target:self action:@selector(closeKeyboard:)];      [keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, accept, nil]]; }  self.txtDate.inputAccessoryView = keyboardToolbar;  datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeDate; [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];  self.txtDate.inputView = datePicker;   - (void)datePickerValueChanged:(id)sender{  date = datePicker.date;  NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd/MM/YYYY"];   [self.txtDate setText:[df stringFromDate:date]]; } 

in storyboard add to txtDate -> EditingDidEnd

- (IBAction)establishHour:(id)sender{  hour = hourPicker.date;  NSDateFormatter *hf = [[NSDateFormatter alloc] init]; [hf setDateFormat:@"HH:MM"];  [self.txtHour setText:[hf stringFromDate:hour]];  } 

Sets the keyboard as DatePicker and created a toolbar with a button to accept the selection.

like image 45
user1617027 Avatar answered Sep 22 '22 05:09

user1617027