2010-09-15 78 views
2

我正在嘗試將CPXYGraph加載到拆分視圖控制器的細節視圖中。當它試圖顯示繪圖數據時,我得到一個EXC_BAD_ACCESS。拆分視圖的細節視圖中的CorePlot

我創建了一個基於「基於分割視圖的應用程序」的新項目。添加CorePlot框架後,我做了以下修改:

1-添加GraphController(.m,.h和.xib)。 xib包含一個具有CPLayerHostingView類型的從屬視圖的UIView。 2-下面的行添加到應用程序委託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; 
    } 
} 

違規行是[圖a ddPlot: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」. 

任何人都可以幫忙嗎?

回答

0

它看起來並不像你在任何地方保留數據數組。嘗試將最後聲明

plotData = [newData retain]; 

,或者,如果你有這方面定義的屬性,

self.plotData = newData; 

埃裏克

+0

沒有工作。同樣的問題。 – DLS 2010-09-16 14:05:31

+0

我在沒有拆分視圖的項目中運行相同的(CP)代碼,它可以工作。儘管如此,我一定希望將分割視圖作爲選項。 – DLS 2010-09-16 14:23:09

相關問題