2012-11-18 15 views
0

請問有人可以概述這條線究竟做了什麼?這個目標C線做什麼?代表,自我,didSelectDate

[self.delegate calendar:self didSelectDate:self.selectedDate]; 

這條線基本上是用來設置在另一個類中的標籤dateLabel上的日期。

CKCalendarView *calendar = [[CKCalendarView alloc] initWithStartDay:startMonday]; 
    calendar.delegate = self; 

    self.dateFormatter = [[NSDateFormatter alloc] init]; 
    [self.dateFormatter setDateFormat:@"dd/MM/yyyy"]; 
    calendar.selectedDate = [self.dateFormatter dateFromString:@"18/11/2012"]; 
    calendar.minimumDate = [self.dateFormatter dateFromString:@"09/11/2012"]; 
    calendar.maximumDate = [self.dateFormatter dateFromString:@"29/11/2012"]; 

    calendar.frame = CGRectMake(10, 10, 300, 320); 
    [self.view addSubview:calendar]; 

    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(calendar.frame) + 4, self.view.bounds.size.width, 24)]; 
    [self.view addSubview:self.dateLabel]; 

'夫婦線'詳細解釋了上面的行會有什麼幫助。

回答

2

簡而言之,你的日曆有一個CKCalendar對象的實例,這個視圖看起來是UIView類之一的子類(我假設,因爲我們不能說因爲你沒有包括一個。 h文件或完整的代碼從你正在使用這個.m文件)。

UIView後代的大多數類都需要委託人來處理用戶界面中的行爲並由其生成。 (有關代表所做的詳細說明,請參閱How does a delegate work in objective-C?)。請注意,您明確設置你實例化calendar後立即委託:

calendar.delegate = self 

因此,與那行,你做這個類顯示和委託讓你的類需要實現什麼方法的協議要求(一個這看起來是 -(void)calendar:didSelectDate:。)

的代碼,你引用的行說(基本)「使用在委託發現-(void)calendar:didSelectDate:方法,並通過自身和selectedDate作爲該方法的參數/參數。」

我寫了一個路線爲:

[[self delegate] calendar:self didSelectDate:[self selectedDate]]; 

希望有所幫助。

+2

在Objective-C中,方法名稱實際上是'-calendar:didSelectDate:',如第3段所述,調用'calendar:'和'didSelecteDate:'兩種方法並不十分準確。一個委託方法'-calendar:didSelectDate:'它接受兩個參數,日曆和日期。除此之外,這是一個很好的描述。 – gaige

+0

你是對的@gaige。我應該說得更清楚。我會編輯我的答案以反映這一點。雖然我不想向OP傳達不正確的信息,但我想盡量清楚地向他們解釋代表的情況。在這樣做的過程中,我對被調用方法的正確閱讀並不清楚。感謝您的協助! –