0

我是 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

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

我真的很感激你的回答。

4

1 回答 1

0

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

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

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

dd - 日 MM - 月 yyyy - 年 HH - 小时 mm - 分钟 ss - 秒

...所以 [formatter setDateFormat:@"MM/dd/yyyy, HH:mm"] 将为您提供类似“01/01/2013, 13:45”的信息。

于 2013-08-13T15:12:53.250 回答