Chandu
Chandu

Reputation: 695

Displaying Date in a TextField

I am having a textfield which when tapped pops up a date picker and and the selected date is displayed in the textfield.

my problem is i want todays date to be displayed in the textfield by default before the user tap the textfield. later the user can change it by tapping and selecting someother date according to his requirement.

Upvotes: 0

Views: 8169

Answers (4)

Suresh D
Suresh D

Reputation: 4305

You can use like this..

textfield.text=[NSString stringWithFormat:@"%@",[NSDate date]];

or

textField.placeholder=[NSString stringWithFormat:@"%@",[NSDate date]];

When the UITextfield value Empty (textfield by default before the user tap the textfield), the placeholder value will display.

Upvotes: 3

Denis
Denis

Reputation: 6413

if I got your idea correctly, you need to use placeholder property of the textfield

NSString *dateString = [dateFormatter stringFromDate: [NSDate date]];
textField.placeholder = dateString;

Placeholder value will be displayed in textfield, while it's value is empty.

Upvotes: 1

visakh7
visakh7

Reputation: 26400

Todays date can be obtained by NSDate *today = [NSDate date]; Assign the string from this NSDate object to your text field. The string can be obtained using NSDateFormatter.

Upvotes: 0

Minakshi
Minakshi

Reputation: 1433

Take the date and assign it to the textField.text and again when user picks the value then again change the value of textField.text

   NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
 textField.text = dateString;

If you have pickerview of dates then in respective function assign the selected value to textField.text Thats it.

Upvotes: 3

Related Questions