当我尝试编译以下代码时,出现以下错误:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIView setHostedGraph:]:无法识别的选择器已发送到实例0x6768c10'

码:

 UIView *ChartView;

  ChartView = [[UIView alloc] init];
  graph = [[CPXYGraph alloc] initWithFrame: ChartView.bounds];

CPGraphHostingView *hostingView = (CPGraphHostingView *)ChartView;
hostingView.hostedGraph = graph;

有什么事吗

最佳答案

您正在将UIView实例(不响应-setHostedGraph:)强制转换为CPGraphHostingView。 -这将不起作用

您需要创建一个实际的CPGraphHostingView对象,然后在其上调用-setHostedGraph:

因此,您的代码应如下所示:

CGRect someFrame = ...;
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:someFrame];
graph = [[CPXYGraph alloc] initWithFrame: hostingView.bounds];

hostingView.hostedGraph = graph;

10-08 05:45