我有一个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为此提供了一种机制。你应该能够设置另一个视图(如日期选择器)作为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. 用按钮编辑UIDatePicker?
- 3. 使用iOS进行编辑以调整UITextField的高度Autolayout
- 4. UIDatePicker没有更新UITextField
- 5. 编辑UITextField时显示UIPickerView
- 6. UITextField编辑问题 - iPhone SDK
- 7. UITextField编辑后检查空
- 8. 如何编辑的UITextField
- 9. 的UITextField不编辑正确
- 10. iOS Swift 3 - UIDatePicker
- 11. 用UIStepper编辑UITextField字体大小
- 12. IOS:禁用UItextfield
- 13. iOS UIDatePicker set max date
- 14. UIDatePicker后台iOS 8
- 15. iOS:编辑Addressframework
- 16. 使用iOS 5富文本编辑器
- 17. UITextField编辑开始呼吁查看结束编辑
- 18. 当使用自定义UITableViewCell编辑之前更改UITextField文本
- 19. 使用Swift在UITextField中的不可编辑的前缀
- 20. 的UITextField不能编辑,重点
- 21. UITextField编辑和添加到数组
- 22. UITextField始终是可编辑的
- 23. Xamarin UIView中可编辑的UITextField
- 24. UITextField在编辑时更改字体
- 25. 的UITextField编辑部分字符串
- 26. 禁止仅对UITextField进行编辑?
- 27. 编辑时不显示UITextField阴影
- 28. 如何设置编辑=否;在UITextField中?
- 29. 无法编辑UITextField中的文本
- 30. 编辑UITableViewCell内的UITextField失败
这种方法存在问题,如果您只想在屏幕底部使用日期选择器,而不是覆盖整个屏幕。你不能让你的模态表示视图控制器透明,因为底层视图不会在那里。 http://stackoverflow.com/questions/849458/transparent-modal-view-on-navigation-controller – kris 2011-11-06 01:01:23