2011-04-19 67 views
0

我有這樣的代碼IOS:模擬日曆

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
[components setYear:2011]; 
[components setDay:1]; 
[components setMonth:7]; 
NSCalendar *gregorianCalendar = [[[NSCalendar alloc]  initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDate *date = [gregorianCalendar dateFromComponents:components]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd"]; 
NSString *strDate = [dateFormatter stringFromDate: date]; 

,但現在我要爲我的一個月,每天設置31個標籤:我如何爲每個標籤加入這種成分?

[components setDay:1]; 

,我也想,所有的星期天是紅色的,然後艾利7天標籤必須是紅色的;你可以幫我嗎?

+0

我看不出有任何代碼的標籤。 – Eiko 2011-04-19 07:51:27

+0

我不知道如何爲我的31個標籤做代碼;我可以設置第一個標籤first.text = strDate; 「第一」是我的標籤;但是之後?換其他標籤?我必須給[component setDay:1]添加一個值;沒有? – CrazyDev 2011-04-19 07:53:49

回答

2

循環是您的朋友:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
[components setYear:2011]; 
[components setDay:1]; 
[components setMonth:7]; 
NSCalendar *gregorianCalendar = [[[NSCalendar alloc]  initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDate *firstDate = [gregorianCalendar dateFromComponents:components]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd"]; 

for (int i = 0; i < 31; i++) { 
    NSTimeInterval seconds = 24*60*60 * i; 
    NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate]; 
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; 
    int weekday = [weekdayComponents weekday]; 
    NSString *strDate = [dateFormatter stringFromDate: date]; 
    yourLabel.text = strDate; 
    yourLabel.textColor = weekday == 1 ? [UIColor redColor : blackColor]; 
} 

請調整yourLabel變量到合適的人。

+0

是的......但爲什麼我必須做一個「爲」?我有31個標籤:first.text,second.text,third.text ecc ...每個標籤都有它的價值;當我做這件事情時,我會創建一個IBAction,在那裏我可以添加一個月到我的標籤,所以當我按下按鈕時,所有標籤都會在下個月更改它的值... – CrazyDev 2011-04-19 08:32:42

+0

當然,您不必使用循環。它只是爲你節省了一百行代碼。但是,您仍然需要使用數組或類似的標籤來方便地訪問它們。 – Eiko 2011-04-19 08:40:03

0

這就好比@英子的回答,但更正確的事情就像日光節約時間,幾個月內不會有31天,等打交道時:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
[components setYear:2011]; 
[components setMonth:7]; 
[components setDay:1]; 

NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDate *startDate = [gregorianCalendar dateFromComponents:components]; 
NSRange rangeOfDays = [gregorianCalendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:startDate]; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd"]; 

for (NSInteger i = 1; i <= NSMaxRange(rangeOfDays); ++i) { 
    [components setDay:i]; 
    NSDate *date = [gregorianCalendar dateFromComponents:components]; 

    yourLabel.text = [dateFormatter stringFromDate: date]; 

    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; 
    int weekday = [weekdayComponents weekday]; 

    yourLabel.textColor = weekday == 1 ? [UIColor redColor : blackColor]; 
} 

[dateFormatter release];