xcode 在文本字段中显示日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8061418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:22:33  来源:igfitidea点击:

Displaying Date in a TextField

iphoneiosxcodeuitextfielduidatepicker

提问by Chandu

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.

我的问题是我希望今天的日期在用户点击文本字段之前默认显示在文本字段中。稍后用户可以通过点击并根据他的要求选择其他日期来更改它。

回答by Minakshi

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

获取日期并将其分配给 textField.text 并在用户选择值时再次更改 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.

如果您有日期的选择视图,则在相应的函数中将选定的值分配给 textField.text 就是这样。

回答by Suresh D

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.

当 UITextfield 值为 Empty(默认情况下在用户点击 textfield 之前的 textfield)时,将显示占位符值。

回答by Denis

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.

占位符值将显示在文本字段中,而它的值为空。

回答by visakh7

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

可以通过NSDate *today = [NSDate date];将此NSDate对象中的字符串分配给文本字段来获取今天的日期。可以使用 获取字符串NSDateFormatter