CorePlot里面拆分视图的详细视图

时间:2010-09-15 20:21:57

标签: iphone ipad uisplitviewcontroller core-plot

我正在尝试将CPXYGraph加载到拆分视图控制器的详细视图中。我在尝试显示绘图数据时收到了EXC_BAD_ACCESS。

我基于“基于拆分视图的应用程序”创建了一个新项目。添加CorePlot框架后,我做了以下修改:

1-添加一个GraphController(.m,.h和.xib)。 xib包含一个UIView,其下级视图类型为CPLayerHostingView。 2-将以下行添加到app delegate didFinishLaunchingWithOptions

 [detailViewController performSelector:@selector(configureView) withObject:nil afterDelay:0];

3-将以下内容添加到DetailViewController configureView

 CGRect graphFrame = CGRectMake(0, 43, 662, 450);
 GraphController *graphController = [[[GraphController alloc]
   initWithNibName:@"GraphController" bundle:nil] autorelease];
 [graphController.view setFrame:graphFrame];
 [self.view addSubview:graphController.view];
 [graphController reloadData];

4- GraphController中的reloadData方法几乎是从其中一个CorePlot示例(DatePlot)粘贴的,我将在这里复制并粘贴(大部分) -

-(void)reloadData
{
    if (!graph)
    {
        [self parentViewController];
        [self.view addSubview:layerHost];

        // Create graph from theme
        graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
        CPTheme *theme = [CPTheme themeNamed:@"Dark Gradients"];
        [graph applyTheme:theme];
        ....
        [layerHost setHostedLayer: graph];
        ....
        // Setup scatter plot space
        CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
        NSTimeInterval xLow = 0.0f;
        plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow) length:CPDecimalFromFloat(oneDay*5.0f)];
        plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(3.0)];

        // Axes
        CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
        CPXYAxis *x = axisSet.xAxis;
        x.majorIntervalLength = CPDecimalFromFloat(oneDay);
        x.orthogonalCoordinateDecimal = CPDecimalFromString(@"2");
        x.minorTicksPerInterval = 0;
        NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        dateFormatter.dateStyle = kCFDateFormatterShortStyle;
        CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
        timeFormatter.referenceDate = refDate;
        x.labelFormatter = timeFormatter;

        CPXYAxis *y = axisSet.yAxis;
        y.majorIntervalLength = CPDecimalFromString(@"0.5");
        y.minorTicksPerInterval = 5;
        y.orthogonalCoordinateDecimal = CPDecimalFromFloat(oneDay);

        // Create a plot that uses the data source method
        CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
        dataSourceLinePlot.identifier = @"Date Plot";
        dataSourceLinePlot.dataLineStyle.lineWidth = 3.f;
        dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
        dataSourceLinePlot.dataSource = self;
        **[graph addPlot:dataSourceLinePlot];**

        // Add some data
        NSMutableArray *newData = [NSMutableArray array];
        NSUInteger i;
        for ( i = 0; i < 5; i++ ) {
            NSTimeInterval x = oneDay*i;
            id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
            [newData addObject:
             [NSDictionary dictionaryWithObjectsAndKeys:
              [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPScatterPlotFieldX], 
              y, [NSNumber numberWithInt:CPScatterPlotFieldY], 
              nil]];
        }
        plotData = newData;
    }
}

违规行是[graph addPlot:dataSourceLinePlot];如果我对此进行评论,模拟器会出现并显示图形的x轴和y轴,当然也没有数据。添加此行将导致以下SIGART -

2010-09-15 14:35:58.959 SplitViewWithCorePlot[17301:207] relabel <<CPScatterPlot: 0x4c458c0> bounds: {{0, 0}, {558, 386}}>
Program received signal:  “EXC_BAD_ACCESS”.

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

看起来你不会在任何地方保留数据阵列。尝试将最后一个语句更改为

plotData = [newData retain];

或者,如果您为其定义了属性,

self.plotData = newData;

埃里克