2011-04-18 70 views
0

我有三十一個標籤,並且這些標籤顯示月份的日期;我想推一個按鈕並更改所有標籤以顯示下個月的日期。例如:如果我有七月的日子顯示,並且我想要更改爲八月,我按下一個按鈕,所有標籤都會更改值。可能嗎?更改按鈕上的標籤文本按

+0

確定這是可能的。你的問題到底是什麼?你有什麼嘗試?什麼工作,什麼沒有? – 2011-04-18 13:56:57

+0

我想知道我該如何製作它 – CrazyDev 2011-04-18 13:59:08

回答

1

是的,這是可能的。你可能應該看看NSCalendarNSDateComponents

您可以使用NSDateComponents這樣的:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
[components setYear:2011]; 
[components setDay:1]; 
[components setMonth:month]; 
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDate *date = [gregorianCalendar dateFromComponents:components]; 

,你每按一次按鈕,增加一個月變量。
NSDateComponents的好處在於組件是封裝的,所以如果您將月份設置爲24,您將在未來獲得2年的日期。

+0

以及如何將我的標籤與組件鏈接? – CrazyDev 2011-04-18 14:13:00

+0

使用[NSDateFormatter](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html)格式化日期並設置[text屬性](http://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/doc/uid/TP40006797-CH3-SW6),結果爲UILabel從'[dateFormatter stringFromDate:...]' – 2011-04-18 14:41:00

+0

啊好的謝謝.... – CrazyDev 2011-04-18 14:42:33