2010-09-22 44 views
0

我在時間模式下使用UIDatePicker來獲取時間。我試圖顯示在標籤中選擇的時間。以下代碼不起作用如何顯示來自UIDatePicker的時間

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
[outputFormatter setDateFormat:@"h:mm a"]; 

label.text = [outputFormatter stringFromDate:self.datePicker.date]; 

[outputFormatter release]; 

回答

1

該代碼對我來說看起來很好。您是否仔細檢查了您的標籤/日期選擇器是否正確鏈接(通過IB或您創建它們)?另外,代碼的具體做法不起作用?

+0

你說得對鏈接。我忘了在IB中建立鏈接。代碼起作用。 – 2010-09-22 04:55:51

1

看來你只是試圖在setDateFormat中設置時間格式。

將以下函數作爲選擇器方法調用 - 已解決。

- (void)changeDateInLabel:(id)sender{ 

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    [df setDateStyle:NSDateFormatterMediumStyle]; 
    [df setDateStyle:NSDateFormatterShortStyle]; 
    [df setDateFormat:@"MM/dd/yyyy"]; 
    [df setTimeZone:@"h:mm a"]; 

    }