如何使用日期选择器显示与特定日期相关的特定文本?

时间:2013-08-13 14:58:56

标签: ios datepicker

我是iOS的新手,我不熟悉它的所有API。我发现了如何在文本字段中显示所选日期,但我不知道写什么来显示与特定日期相关的特定(选定)文本。

这是我用来显示日期的代码。你知道我需要改变什么才能把我自己的文本写到特定的日期吗?:

 #import "ViewController.h"

 @implementation ViewController
 @synthesize datePicker;
 @synthesize dateLabel;

 - (void)didReceiveMemoryWarning
 {
     [super didReceiveMemoryWarning];
     // Release any cached data, images, etc that aren't in use.
 }

 #pragma mark - View lifecycle

 - (void)viewDidLoad
 {
     [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 }

 - (void)viewDidUnload
 {
     [self setDateLabel:nil];
     [self setDatePicker:nil];
     [super viewDidUnload];
     // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
 }

 - (void)viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:animated];
 }

 - (void)viewDidAppear:(BOOL)animated
 {
     [super viewDidAppear:animated];
 }

 - (void)viewWillDisappear:(BOOL)animated
 {
    [super viewWillDisappear:animated];
 }

 - (void)viewDidDisappear:(BOOL)animated
 {
    [super viewDidDisappear:animated];
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
 {
     // Return YES for supported orientations
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
 }

 - (IBAction)touchButton:(id)sender {

     NSDate *dateSelect = [datePicker date];
     NSString *dateStamp = [[NSString alloc]initWithFormat:@"The date is %@", dateSelect];

     dateLabel.text = dateStamp;
 }
 @end

(我有一个按钮,日期选择器和文本字段)

我非常感谢你的回答。

1 个答案:

答案 0 :(得分:0)

您必须使用格式化程序。看看......

...
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"]; <--- This will show hours and minutes
NSString *dateStamp = [formatter stringFromDate:date];
...

在setDateFormat中,您可以通过这种方式设置所需的输出格式......

dd - day MM - 月 yyyy - 年 HH - 小时 毫米 - 分钟 ss - 第二次

...所以[formatter setDateFormat:@“MM / dd / yyyy,HH:mm”]会得到类似“01/01 / 2013,13:45”的内容。

相关问题