在文本框上点击时显示dateselect器
如何显示一个文本框点击一个datetimepicker控件?
我有一个用户界面,有到达和离开的文本字段,当用户点击到达文本框时,应该popup一个datetimepicker控件,而不是键盘和相同的出发文本框。
您可以使用inputView
本字段的inputView
和inputAccessoryView
属性。 创builddateselect器并将其设置为两个文本字段的input视图。 还为“ Done
button创build另一个视图,并将其作为附件视图。 您将需要该button来closuresinput视图。
Done
button必须连接到基本上这样做的function –
if ( [textField1 isFirstResponder] ) { [textField1 resignFirstResponder]; } else if ( [textField2 isFirstResponder] ) { [textField2 resignFirstResponder]; }
另一个选项是inputView
UITextField
并覆盖inputView
和inputAccessoryView
。 当有负载的时候,这是要走的路。
例
@interface CustomKeyboardAppDelegate : NSObject <UIApplicationDelegate> { ... @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITextField *textField; @property (nonatomic, retain) IBOutlet UIToolbar *accessoryView; @property (nonatomic, retain) IBOutlet UIDatePicker *customInput; - (IBAction)dateChanged:(id)sender; - (IBAction)doneEditing:(id)sender; @end
在XIB中,拔出UIToolbar
和UIDatePicker
但不要将其附加到视图。 适当连接sockets。 dateChanged:
响应dateselect器和doneEditing:
当单击工具栏中的Done
button时调用。 连接它们也是。 这些方法的实现如下所示。
@implementation CustomKeyboardAppDelegate @synthesize window=_window; @synthesize textField = _textField; @synthesize accessoryView = _accessoryView; @synthesize customInput = _customInput; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.textField.inputView = self.customInput; self.textField.inputAccessoryView = self.accessoryView; ... } ... - (IBAction)dateChanged:(id)sender { UIDatePicker *picker = (UIDatePicker *)sender; self.textField.text = [NSString stringWithFormat:@"%@", picker.date]; } - (IBAction)doneEditing:(id)sender { [self.textField resignFirstResponder]; } @end
最后两个方法会膨胀,因为更多的文本字段依赖于这个select器。
很简单,在ViewController.h
把UITextFieldDelegate
放在viewDidLoad
。
txtFecha.delegate = self; datePicker = [[UIDatePicker alloc]init]; [datePicker setDatePickerMode:UIDatePickerModeDate]; self.txtFecha.inputView = datePicker;
在 ViewController.m的 viewDidLoad中
- (void)viewDidLoad { [super viewDidLoad]; UIDatePicker *datePicker = [[UIDatePicker alloc]init]; [datePicker setDate:[NSDate date]]; datePicker.datePickerMode = UIDatePickerModeDate; [datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged]; [txtFieldBranchYear setInputView:datePicker]; }
在您的ViewController中添加一个方法
-(void) dateTextField:(id)sender { UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView; [picker setMaximumDate:[NSDate date]]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; NSDate *eventDate = picker.date; [dateFormat setDateFormat:@"dd/MM/yyyy"]; NSString *dateString = [dateFormat stringFromDate:eventDate]; txtFieldBranchYear.text = [NSString stringWithFormat:@"%@",dateString]; }
txtFieldBranchYear 是UITextField的出口
我find了一个非常有用的课程 – 博客文章在这里: http : / inputAccessory视图。 踢球者是UILabel,至less是我所做的,正是我所需要的。
打开dateselect器上点击textField swift 3.0添加UITextFieldDelegate并在ViewDidload()上设置委托tfDate.delegate = self
//MARK:- TextField Delegate //MARK:- func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } //MARK:- DatePicker //MARK:- func textFieldDidBeginEditing(_ textField: UITextField) { self.callDatePicker(textField: tfDate) } func callDatePicker(textField:UITextField){ let datePickerView : UIDatePicker = UIDatePicker() datePickerView.datePickerMode = UIDatePickerMode.date textField.inputView = datePickerView datePickerView.addTarget(self, action: #selector(MyViewController.handleDatePicker), for: .valueChanged) } var str:String = "" var str1:String = "" var strStartDate:Date! var strEndDate:Date! func handleDatePicker(sender: UIDatePicker) { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" print("Date :: \(dateFormatter.string(from: sender.date))") tfDate.text = dateFormatter.string(from: sender.date) }