У меня есть uitextfield, где написана дата .. как я могу показать в нижней части страницы uidatepicker вместо стандартной клавиатуры? Заранее спасибоios UITextField для редактирования с uidatepicker
6
A
ответ
4
Убедитесь, что ваш класс реализует протокол UITextFieldDelegate
. Затем переопределить метод shouldBeginEditing
делегата возвращать FALSE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//check if it is the UITextField you don't want the keyboard for
if (textField != dateTextField)
return TRUE;
//present your UIDatePicker
//see instructions below
//Return false to prevent the keyboard from appearing.
return FALSE;
}
Я рекомендую использовать другой UIViewController
, который содержит UIDatePicker
и использовать presentModalViewController
, чтобы показать его. Это соответствует Руководству пользователя.
5
UITextField обеспечивает механизм для этого. Предполагается, что вы должны установить другое представление (например, datepicker) как inputView. Смотрите этот пост:
3
Вы можете использовать следующий код;
//first of all, you have to declare the datePicker and then use the following code;
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[dateLabel resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
и дополнительные действия;
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:@"en_US"]];
[FormatDate setDateFormat:@"dd.MM.yyyy"];
dateLabel.text = [FormatDate stringFromDate:[datePicker date]];
}
-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[dateLabel resignFirstResponder];
return YES;
}
-(IBAction)doneButtonClicked{
[self closeDatePicker:self];
}
Смежные вопросы
- 1. UIDatePicker для установки текста UITextfield
- 2. UITableView + UITextField + UIDatePicker
- 3. UITextField и UIDatePicker
- 4. UIDatePicker не обновляет UITextField
- 5. несколько UIDatePicker one uitextfield
- 6. Как программно выбрать UITextField для редактирования
- 7. Использование UIDatePicker вместо клавиатуры для проблем UITextField
- 8. Правильное отображение UIDatePicker без UITextField
- 9. Замена ввода UITextField с помощью UIDatePicker
- 10. UITextField автоматически начинает режим редактирования
- 11. Использование UIDatePicker с UITextField без текстового поля
- 12. UIDatePicker в сочетании с UITextField, didselectrow
- 13. iOS 7: UITextField inputView проблема при установке на UIDatePicker
- 14. IOS - UIDateView к UITextField
- 15. Как добавить дату рождения UIDatePicker в UITextField?
- 16. iOS Swift 3 - UIDatePicker
- 17. UITextField iOS CustomKeyboard
- 18. UIDatePicker background iOS 8
- 19. Настройка UITextField недоступна для редактирования или редактируется
- 20. UIDatePicker width issue IOS
- 21. Прокрутка без редактирования в UITextField
- 22. Ошибка приложения в ValueChanged UIDatePicker для UITextField в UIAlertController
- 23. Как добавить текст после редактирования конца UITextfield в iOS
- 24. Приложение для сброса UIDatePicker
- 25. iOS UITextField resignFirstResponder issue
- 26. UITextField Text Invisible До редактирования
- 27. Как вызвать метод на UITextField после редактирования в iOS
- 28. IOS - Реализация класса для UITextField
- 29. Автозаполнение для UITextfield в iOS
- 30. tvOS UITextField пустой после редактирования
есть проблема с этим подходом, если вы только хотите, чтобы дампикер находился в нижней части экрана, а не охватывал весь экран. Вы не можете сделать свой прозрачный прозрачный диспетчер изображений прозрачным, потому что базовых представлений не будет. http://stackoverflow.com/questions/849458/transparent-modal-view-on-navigation-controller – kris