【发布时间】:2019-01-02 11:47:43
【问题描述】:
我正在制作一个程序来绘制图表(我保留在 NSView 类中)。但是我想从 NSViewController 传递动作和数据。所以你能帮我怎么做。我确实尝试了以下代码,但它不起作用。
@implementation PlottingChart
@synthesize plotChartData;
- (void)drawRect:(NSRect)dirtyRect{
[super drawRect:dirtyRect];
[self drawChartGrid:plotChartData];
}
-(void)drawChartGrid:(NSMutableArray *)ChartData
{
//Drawing code here
}
@interface PlottingChart : NSView
@property (nonatomic, strong) NSMutableArray *plotChartData;
-(void)drawChartGrid:(NSMutableArray *)ChartData;
@end
#import "PlottingChart.h"
@interface ViewController :NSViewController<NSTableViewDataSource,NSTableViewDelegate>
{
PlottingChart *boxPlotChart;
}
- (IBAction)btnStart:(id)sender {
//trial draw chart
NSDictionary *dict1 = @{@"plot_Q1":@"180",@"plot_Q3": @"220", @"plot_Max":@"250", @"plot_Min":@"150", @"plot_Median":@"200"};
NSDictionary *dict2 = @{@"plot_Q1":@"190",@"plot_Q3": @"230", @"plot_Max":@"280", @"plot_Min":@"160", @"plot_Median":@"210"};
NSMutableArray *array = [NSMutableArray arrayWithObjects:dict1,dict2, nil];
boxPlotChart.plotChartData = array;
[boxPlotChart drawChartGrid:array];
boxPlotChart = [[PlottingChart alloc] initWithFrame:NSMakeRect(10, -50, 850, 360) ]; // x,y lenght,height
[self.view addSubview:boxPlotChart];
}
【问题讨论】:
-
您能解释一下为什么将
boxPlotChart与boxPlotChart.plotChartData = array;和[boxPlotChart drawChartGrid:array];一起使用,然后创建一个没有数据的新PlottingChart吗? -
我只是尝试将数组从 ViewController 传递到 PlottingChart 视图。但似乎它不起作用。
-
为什么你有这两行
boxPlotChart.plotChartData = array; [boxPlotChart drawChartGrid:array];它们看起来是多余的,你正在设置数组然后用同一个数组的参数绘制,你要么将数组添加到属性boxPlotChart.plotChartData = array然后绘制 - 您已经在plotChartData属性中拥有数组 -
我确实尝试了 3 种情况,其中一种是:boxPlotChart.plotChartData = array ;仅其他一个:[boxPlotChart drawChartGrid:array];最后一个是两者。似乎 3 方式无法将数组传递给 boxPlotChart。在调试期间,我也没有看到任何数组通过 boxPlotChart。你有什么建议吗,
-
您能否将
btnStart方法中的代码更新为您当前拥有的代码,因为您之前拥有的代码由于调用顺序而没有任何意义
标签: objective-c macos nsview nsviewcontroller