错误:请求成员'bounds'不是结构或联合

时间:2009-10-19 21:00:05

标签: iphone uiview interface-builder core-plot

使用core-plot框架编译iPhone应用程序时出现上述错误。我有这个视图控制器的视图链接到IB中的CPLayerHostingView。以下是viewDidLoad()函数的示例代码。

- (void)viewDidLoad {
    [super viewDidLoad];

    graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds];

    CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
    hostingView.hostedLayer = graph;


    graph.paddingLeft = 20.0;
    graph.paddingTop = 20.0;
    graph.paddingRight = 20.0;
    graph.paddingBottom = 20.0;


    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-6)
                                                   length:CPDecimalFromFloat(12)];
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-5)
                                                   length:CPDecimalFromFloat(30)];

    CPLineStyle *lineStyle = [CPLineStyle lineStyle];
    lineStyle.lineColor = [CPColor blackColor];
    lineStyle.lineWidth = 2.0f;

    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
    axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
    axisSet.xAxis.minorTicksPerInterval = 4;
    axisSet.xAxis.majorTickLineStyle = lineStyle;
    axisSet.xAxis.minorTickLineStyle = lineStyle;
    axisSet.xAxis.axisLineStyle = lineStyle;
    axisSet.xAxis.minorTickLength = 5.0f;
    axisSet.xAxis.majorTickLength = 7.0f;
    axisSet.xAxis.axisLabelOffset = 3.0f;

    axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
    axisSet.yAxis.minorTicksPerInterval = 4;
    axisSet.yAxis.majorTickLineStyle = lineStyle;
    axisSet.yAxis.minorTickLineStyle = lineStyle;
    axisSet.yAxis.axisLineStyle = lineStyle;
    axisSet.yAxis.minorTickLength = 5.0f;
    axisSet.yAxis.majorTickLength = 7.0f;
    axisSet.yAxis.axisLabelOffset = 3.0f;

    CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];
    xSquaredPlot.identifier = @"X Squared Plot";
    xSquaredPlot.dataLineStyle.lineWidth = 1.0f;
    xSquaredPlot.dataLineStyle.lineColor = [CPColor redColor];
    xSquaredPlot.dataSource = self;
    [graph addPlot:xSquaredPlot];

    CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
    greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);

    [xSquaredPlot setPlotSymbol:greenCirclePlotSymbol];

    CPScatterPlot *xInversePlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];
    xInversePlot.identifier = @"X Inverse Plot";
    xInversePlot.dataLineStyle.lineWidth = 1.0f;
    xInversePlot.dataLineStyle.lineColor = [CPColor blueColor];
    xInversePlot.dataSource = self;
    [graph addPlot:xInversePlot];
}

麻烦的是这些:

CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];
CPScatterPlot *xInversePlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];

它说graph.defaultPlotSpace没有'界限'。有没有人遇到这个?

3 个答案:

答案 0 :(得分:1)

尝试使用括号语法调用这些调用。

CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc] initWithFrame:[[graph defaultPlotSpace] bounds]] autorelease];
CPScatterPlot *xInversePlot = [[[CPScatterPlot alloc] initWithFrame:[[graph defaultPlotSpace] bounds]] autorelease];

答案 1 :(得分:1)

你正在回归的CPXYPlotSpace,因为图的defaultPlotSpace是CPPlotSpace的子类,它只是NSObject的子类。这些类都没有定义bounds属性。相反,CPXYPlotSpace让您通过CPPlotRange对象设置范围,如检索defaultPlotSpace后的前两行所示。

执行所需操作的正确方法是分配绘图并使用-init,而不是-initWithFrame:。当这些图添加到图表中时,我相信它们会根据图表空间自动调整大小。或者,您可以使用-initWithFrame:并使用图形的框架。

如果您希望查看从CPXYPlotSpace返回边界的便捷方法,请在mailing list中告知我们。

答案 2 :(得分:0)

如果没有包含Quartzcore框架,可以解决此错误的问题。