将数组从NSViewController传递到Objective C可可中的NSView

时间:2018-07-25 15:24:05

标签: objective-c macos nsview nsviewcontroller

我正在制作一个程序来绘制图表(我将其保存在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];
}

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方法。

将initWithFrame方法添加到NSView中。

- (id)initWithFrame:(NSRect)frame dataArray:(NSMutableArray *)dtArray;
 {
   self = [super initWithFrame:frame];
   self.plotChartData = dtArray;
   return self;
  }

然后在NSViewController中将其初始化。

boxPlotChart = [[PlottingChart alloc] initWithFrame:NSMakeRect(10, -50, 850, 360) dataArray:array ];