2012-08-24 31 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

相关问题