2012-08-24 29 views
1

我的代碼有效,但我不明白是誰或在哪裏爲我的視圖的數據源代理調用setter。我明白爲什麼調用該代碼使一切正常工作,我只想知道是誰發出了呼叫/發生了什麼。該視圖的報頭是這樣的,用代碼作爲最後一行的重要之一:什麼時候在iOS中調用委託設置器?

@class GraphView; 

@protocol GraphViewDataSource 

-(float)YValueforXValue:(float)xValue; 

@end 

@interface GraphView : UIView 

@property (nonatomic) CGFloat scale; 
@property (nonatomic) CGPoint graphOrigin; 
@property (nonatomic, weak) IBOutlet id <GraphViewDataSource> dataSource; 

@end 

這符合協議視圖控制器:

@interface GraphViewController() <GraphViewDataSource> 

@property (nonatomic, weak) IBOutlet GraphView *graphview; 

@end 

@implementation GraphViewController 

@synthesize graphview = _graphview; 
@synthesize program = _program; 

-(void)setGraphview:(GraphView *)graphview { 
    _graphview = graphview; 
    self.graphview.dataSource = self; 
} 

我已經排除所需的協議方法和更多,因爲它不相關。我想知道的是誰調用上述setGraphView方法。不幸的是,我無法從斷點獲得幫助(除了知道它被調用)。

同樣,代理首先獲取此代碼視圖中引用:

for (CGFloat thisPointViewXValue=self.bounds.origin.x; thisPointViewXValue<=self.bounds.size.width; thisPointViewXValue +=1/self.contentScaleFactor) 
    { 
     if (FirstPoint) { 
      CGFloat firstpointGraphXValue = [self convertViewXValueToGraphXValue:thisPointViewXValue]; 
      CGFloat firstpointGraphYValue = [self.dataSource YValueforXValue:firstpointGraphXValue]; 
      CGFloat firstpointViewYValue = [self convertGraphYValueToViewY:firstpointGraphYValue]; 
      CGContextMoveToPoint(context, thisPointViewXValue, firstpointViewYValue); 
      FirstPoint = NO; 

     } 
     CGFloat thisPointGraphXValue = [self convertViewXValueToGraphXValue:thisPointViewXValue]; 
     CGFloat thisPointGraphYValue = [self.dataSource YValueforXValue:thisPointGraphXValue]; 
     CGFloat thisPointViewYValue = [self convertGraphYValueToViewY:thisPointGraphYValue]; 
     CGContextAddLineToPoint(context, thisPointViewXValue, thisPointViewYValue); 


} 

那是它發生在哪裏???

回答

0

graphView和dataSource iVars標記爲IBOutlets,即Interface Builder Outlet。

這通常表明GraphViewController經由筆尖/ XIB文件中加載,並且筆尖文件內有從筆尖文件內的其他對象這些IVAR連接。

所以它是在這些iVar上調用setter的nib加載機制。

+0

謝謝,我想這可能是引擎蓋下的東西,但我想確保我沒有遺漏任何東西。 –

+0

如果您想了解更多關於它的工作原理,請閱讀資源編程指南。很高興知道:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW8 – bandejapaisa

0

there self.graphview.dataSource = self;

+0

不,這不是我的問題。我看到那行代碼可以建立連接,但是誰首先調用該setter? –

+0

我認爲如果您發佈完整的代碼會更容易。 – ABLX

相關問題