2014年10月29日 星期三

使用DataSource接外部資料提供自己的模組之用

模組平常都是用delegate去呼叫外部method來當作Callback使用,然而我們也可以利用delegate來作為DataSource去接外部資料回來使用。

//在.m中這樣呼叫你由外部輸入的Data
NSString *myKey = [self.dataSource getMyKey:self];

//在.h中這樣宣告
@protocol ReportChartDataSource;

@interface ReportChart : UIView

@property (weak, nonatomic) id<ReportChartDataSource> dataSource;

@property (nonatomic, readonly) ReportDuration duration;

- (void)reloadData;
- (void)reloadDataWithDuration:(ReportDuration)duration;

@end

//宣告實作
@protocol ReportChartDataSource <NSObject>

- (NSInteger) getMyKey:(ReportChart *)chart;

@end

沒有留言:

張貼留言