-3

enter image description here

After Clicked...Date is not shown in textfield. Textfield is in custom cell of tableview.

enter image description here

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Nikunj Gangani
  • 224
  • 2
  • 10
  • When you hit done, get the date from the pickerView and use NSDateFormatter to format it into the text. Place the resulting text in the textField. – Brandon Sep 01 '16 at 13:12
  • There is so much wrong with this question. You did not even bother to do preliminary research prior to asking the question. You haven't tried anything at all. You haven't even bothered to mention which language are you using. This is a basic question which has been answered trillions of times, go google it – NSNoob Sep 01 '16 at 13:15
  • Possible duplicate of [iPhone display date picker on UITextField touch](http://stackoverflow.com/questions/11197855/iphone-display-date-picker-on-uitextfield-touch) – Avijit Nagare Sep 01 '16 at 13:16
  • Follow this tutorial: http://www.swiftdevcenter.com/uidatepicker-as-input-view-to-uitextfield-swift/ – Ashish Chauhan Apr 07 '19 at 04:28

2 Answers2

0
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[txtField_date setInputView:datePicker];

-(void) dateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)txtField_date.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
txtField_date.text = [NSString stringWithFormat:@"%@",dateString];
}

Try this code.

Amit Srivastava
  • 1,105
  • 9
  • 18
  • Where do you get `txtFieldBranchYear.inputView`? – NSNoob Sep 01 '16 at 13:20
  • 1
    Also, why are you setting `[picker setMaximumDate:[NSDate date]];` in ValueChangedEvent? You should set it before displaying your DatePicker – NSNoob Sep 01 '16 at 13:21
  • if you not set the date picker maximum date just forget this line and use this code. – Amit Srivastava Sep 01 '16 at 13:24
  • 1
    And as told by the OP, the textfield is in a TableViewCell, so it is unlikely that you would have a property named `txtField_date` to access it. You will first have to access the modelClass used to populate the cell, update it and reload the cell. Or if you like bad designs, you could access the cell and then access its subviews. – NSNoob Sep 01 '16 at 13:25
  • you have to already add the textfield in custom cell in tableview. – Amit Srivastava Sep 01 '16 at 13:31
-1

You can use the date property on UIDatePicker and convert it to NSString and set it to your UITextField.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];  // set your own format
NSString *formattedDateString = [formatter stringFromDate:yourPicker.date];
textField.text = formattedDateString;
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
vishnuvarthan
  • 492
  • 1
  • 6
  • 23