何时在iOS中调用委托setter?

时间:2012-08-24 18:31:57

标签: objective-c ios xcode

我的代码有效,但我不明白是谁或在哪里为我的视图的数据源委托调用setter。我理解为什么调用该代码使一切正常,我只想知道是谁调用它/它发生了什么。视图的标题如下所示,最后一行代码是重要的代码:

@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);


}

是发生在哪里???

2 个答案:

答案 0 :(得分:0)

有self.graphview.dataSource = self;

答案 1 :(得分:0)

graphView和dataSource iVars被标记为IBOutlets,即Interface Builder Outlet。

这通常表明GraphViewController是通过nib / xib文件加载的,并且在该nib文件中,有来自nib文件内其他对象的这些iVar的连接。

因此,nib加载机制正在调用那些iVars上的setter。