Currently, I am able to have my keyboard appear when I use a text field in my form. However, I'm not able to hide it. So I always need to shutdown the app and to open it back to change pages. I know there is a way to have like a toolbar to close the Keyboard when you want. However, i don't manage to see how to have this toolbar appear.
However, one of my fields enable a DatePicker View as a Keyboard instead of the natural keyboard.
- (void) initializeTextFieldInputView {
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minuteInterval = 5;
datePicker.backgroundColor = [UIColor whiteColor];
[datePicker addTarget:self action:#selector(dateUpdated:) forControlEvents:UIControlEventValueChanged];
self.DateText.inputView = datePicker;
}
- (void) dateUpdated:(UIDatePicker *)datePicker {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
self.DateText.text = [formatter stringFromDate:datePicker.date];
}
To hide a keyboard either [self.DateText resignFirstResponder] or [self.view endEditing:YES] would work.
To add a toolbar over the keyboard use [self.DateText setInputAccessoryView:yourToolBarView];
Related
I want to show the current date on selecting the row of UIDatePickerView on the label, My issue is when I opened my date picker it doesn't show anything on the label on selecting the row but when I move my date picker view, It started updating my label, I am not able to fix it. I want to display the date whenever I touch the date picker view row. I even added TouchUpInside row method also, still, it is not working
Here is my code snippet:
- (IBAction)btn_calendar_event:(id)sender {
// ADD UIDatePicker and open it
if (self.picker) {
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
}
[self hideKeyboard];
self.picker = [[UIDatePicker alloc] init];
self.picker.backgroundColor = [UIColor whiteColor];
[self.picker setValue:[UIColor blackColor] forKey:#"textColor"];
self.picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.picker.datePickerMode = UIDatePickerModeDate;
forControlEvents:UIControlEventValueChanged];
[self.picker addTarget:self action:#selector(dueDateChanged:)forControlEvents:UIControlEventTouchUpInside];
[self.picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
}
// dueDateChanged method
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
_txtf_birthDate.text = [dateFormatter stringFromDate:[sender date]];
}
// Done button Action
-(void)onDoneButtonClick {
[self dueDateChanged : self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
}
The problem is when you're initialising your picker your picker is not settings the date to label so it'll only update when dueDateChanged will be triggered so when you initialise your date picker select the date you want to display as a default date and show it in label like
After [self.picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged] Simply add a default date to your picker like :-
self.picker.date=[NSDate date]; // add the default initial date
[self dueDateChanged:self.picker];
According to your requirement "If I want to display the current date on selecting row then how to do it, It is now setting default initial date without even selecting the row of date picker view
" you can add this line anywhre from where you want to show the date
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
_txtf_birthDate.text = [dateFormatter stringFromDate:[NSDate date]];
I’d like to show a custom date picker keyboard when tapping a Toolbar button (UIBarButtonItem).
For showing custom keyboards using a a UIResponder’s inputView seems to be the right choice. Unfortunatelly the UIBarButtonItem isn’t a subclass of UIResponder.
How can I have a UIBarButtonItem becomeFirstResponder and carry an inputView to show such a keyboard?
Why do you want to show keyboard when user taps on UIBarButtonItem?
Suppose keyboard is shown on tap of UIBarButtonItem, what will happen when user types in something using keyboard? Which control is expected to receive key press events from keyboard?
Instead of inputView approach, you can follow another approach to show datePicker on button click
Let showDatePicker is the method called on button click then
-(void)showDatePicker
{
UIDatePicker* picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeDate;
[picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
picker.backgroundColor = [UIColor blackColor];
[self.view addSubview:picker];
}
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
//self.myLabel.text = [dateFormatter stringFromDate:[dueDatePickerView date]];
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
}
To remove the datePicker, you can use removeFromSuperView on either method called from another button click in your View or your custom toolbar done button.
I've accomplished this by creating a UITextField
let myPicker = UIPickerView()
lazy var pickerInputField: UITextField = {
let field = UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
self.view.addsubview(field)
field.delegate = self
return field
}()
func barButtonItemTouched(sender: Any) {
// dismiss the picker if it's showing
if pickerInputField.isFirstResponder {
pickerInputField.resignFirstResponder()
}
pickerInputField.inputView = myPicker
pickerInputField.becomeFirstResponder()
}
I've been using a multitide of tutorials and stackoverflow answers to piece this together. I have a UITextField which when the user taps slides up a UIDatePicker (instead of keyboard). Once the datepicker is closed the TextField should update with the selected date.
However it just has the current date/time in there no matter what you change it to. What am I missing? I want it formatted as MM / dd / yyyy too.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//birthday view
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
UIToolbar *doneBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
_birthdayField.inputView = datePicker;
[_birthdayField setInputAccessoryView:doneBar];
[doneBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[doneBar setItems: [NSArray arrayWithObjects:spacer, [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(datePickerDone:)],nil ] animated:YES];
}
- (IBAction)datePickerDone:(id)sender {
[_birthdayField resignFirstResponder];
}
-(void)updateTextField:(id)sender {
if([_birthdayField isFirstResponder]) {
UIDatePicker *picker = (UIDatePicker*)_birthdayField.inputView;
_birthdayField.text = [NSString stringWithFormat:#"%#", picker.date];
}
}
I assume it's something I'm missing in updateTextField.
Because you resign the _birthdayField when the done button is touched.
So the code in updateTextfield if block never gets called.
After playing around I figured out the issue. Bseh's answer didn't apply as when I placed a breakpoint in updateTextField it was definitely hitting it. I changed the following:
- (void)updateTextField:(UIDatePicker *)sender {
NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
[datePickerFormat setDateFormat:#"MM / dd / yyyy"]; //here specify your format..
NSString *dateString = [datePickerFormat stringFromDate:sender.date];
self.birthdayField.text = dateString;
}
Changing that id to UIDatePicker made the difference for me! I've also added the formatting I wanted.
I have a UITextField in a UITableView. I am trying to have a UIPickerView or a UIDatePicker as its input view. Both options are appearing and in case of UIPickerView, it is loading correct data. The problem is that it is not scrollable and user cannot interact with it, choose and scroll between the various options.
Userinteraction is set to yes, delegates are correct and everything is in place.
This is the code:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
textField.inputView = datepicker;
return YES;
}
Why are you set the inputView in this method?
Do this in viewDidLoad. This is a configuration,
doesn't make sense set a particular inputView each time the user focus on that textField:
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(editTextField:) forControlEvents:UIControlEventValueChanged];
[textField setInputView:datePicker];
}
and then:
- (void)editTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)textField.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
textField.text = [NSString stringWithFormat:#"%#",dateString];
}
I need to add time picker to system settings for my app, Is it possible? I see there is a UITextField but no property with time picker for the UITextField.
You have UIPickerView for showing picker
UITextFieldDelegates for textfield start editing
On click on textfield
Disable Keyboard
Show picker
On picker delegate set the textfield with selected value
Edit : In case of settings page which is managed with a plist.It is impossible to do so since apple doesnt give such wide support on the setings page.You van add settings to the application itself as a setting page and do all this
Initialize a textfield. for example UITextField *txtFld;
//Text Field delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:FRAME];
datePicker.datePickerMode = UIDatePickerModeTime;
[datePicker addTarget:self action:#selector(changeTextFieldValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
-(void)changeTextFieldValue:(id)sender
{
NSDate *date = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *dateString = [dateFormatter stringFromDate:date ];
txtFld.text = dateString;
}