2011-04-18 67 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