访问Core Plot触摸事件的数据阵列时崩溃



我搜索了互联网和这些论坛。我已经检查了GalleryPlot示例代码。我仍然对此感到困扰。

我在iPhone应用程序中使用Core Plot绘制散点图。这些图表画得很漂亮。我做了以下工作:

  • 通过将CPTScatterPlotDatasource和CPTScatterPlotDelegate协议添加到我的View Controller类中,添加了触摸绘图点的功能
  • implemented-(void)scatterPlot:(CPTScatterPlot*)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index{}在我的视图控制器类中

NSLog语句显示正在调用该方法,并且正在发送的索引是一个有效的整数。

如果我添加任何访问包含绘图数据的NSArray的代码,应用程序就会崩溃,并显示EXC_BAD_ACCESS。数据数组是我实现该方法的视图控制器的成员变量。

在尝试访问数组之前,我尝试过重新加载数据,但这并没有解决问题

这是我的委托方法:

- (void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index {
    NSLog(@"Touched by me: index = %d", index);
    [myGraph reloadDataIfNeeded];
    NSNumber* x = [[dataForOurBabyPlot objectAtIndex:index]valueForKey:@"x"];
}

崩溃发生在"NSNumber#x"行。

这是我的视图控制器类声明:

@interface GraphView : UIViewController <CPTScatterPlotDataSource, 
                                         CPTScatterPlotDelegate> {
IBOutlet CPTGraphHostingView* myHostingView;
    CPTXYGraph* myGraph;
    NSArray* dataForOurBabyPlot;
}
@property (nonatomic, retain) IBOutlet CPTGraphHostingView* myHostingView;
@property (nonatomic, retain) NSArray* dataForOurBabyPlot;
// class methods
@end

我错过了什么?

-reloadDataIfNeeded的调用是不必要的——到目前为止,Core Plot已经拥有了所需的所有数据。

EXC_BAD_ACCESS崩溃是由指向坏数据的dataForOurBabyPlot引起的。很可能它被释放了,系统将内存重新用于其他用途。检查您的内存管理,确保阵列在完成之前不会被释放。

最新更新