Reputation: 695
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
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
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
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
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