CorePlot inside detail view of Split View
I'm attempting to load a CPXYGraph into the detail view of a split view controller. I'm getting an EXC_BAD_ACCESS when it attempts to display the plot data.
I create a new project based on "Split View-based application". After adding the CorePlot framework I make the following modifications:
1- add a GraphController (.m, .h and .xib). The xib contains a UIView with a subordinate view of type CPLayerHostingView. 2- add the following line to the app delegate didFinishLaunchingWithOptions
[detailViewController performSelector:@selector(configureView) withObject:nil afterDelay:0];
3- add the following to 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- the reloadData method in GraphController is pretty much pasted from one of the CorePlot samples (DatePlot) and I will copy and paste (most of) it here-
-(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;
开发者_Go百科 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;
}
}
The offending line is [graph addPlot:dataSourceLinePlot]; If I comment this out the simulator comes up and displays the x and y axis of the graph and of course no data. Adding this line back causes the following 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”.
Can anyone help?
It doesn't look like you're retaining the data array anywhere. Try changing the last statement to
plotData = [newData retain];
or, if you have a property defined for it,
self.plotData = newData;
Eric
精彩评论