I have thirty-one labels, and these labels show the days of the month开发者_如何学Go; I want to push a button and change all the labels to display the days of the next month. Example: if I have the days of July showing, and I want change to August, I press a button and all my labels change values. Is it possible?
Yes this is possible. You probably should have a look at NSCalendar and NSDateComponents.
You could use NSDateComponents like this:
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];
and you increment the month variable each time you press a button.
Good thing with NSDateComponents is that the components are wrapped, so if you set month to 24 you'll get a date 2 years in the future.
精彩评论