Objective c 将NSArray从ViewController传递到NSObject类

Objective c 将NSArray从ViewController传递到NSObject类,objective-c,cocoa-touch,nsarray,core-plot,Objective C,Cocoa Touch,Nsarray,Core Plot,我有一个单视图应用程序,其中ViewController包含一个自定义类的UIViewCPTGraphHostingView,以便它保存一个CorePlot图。该UIView的内容由名为SimpleSchetterPlot的NSObject子类管理。此SimpleSchetterPlot对象包含配置图形所需的所有参数(轴范围、标签、点数等) ViewController.h #import <UIKit/UIKit.h> #import "CorePlot-CocoaTouch.h"

我有一个单视图应用程序,其中ViewController包含一个自定义类的UIView
CPTGraphHostingView
,以便它保存一个CorePlot图。该UIView的内容由名为
SimpleSchetterPlot
的NSObject子类管理。此
SimpleSchetterPlot
对象包含配置图形所需的所有参数(轴范围、标签、点数等)

ViewController.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "SimpleScatterPlot.h"

@interface ViewController : UIViewController
{
    IBOutlet CPTGraphHostingView *_graphHostingView;
    SimpleScatterPlot *_scatterPlot;   
    NSMutableData *dataConexion;
}

@property (nonatomic,strong) NSArray *valor;
@property (nonatomic,strong) NSArray *strAnoMes;
@property (nonatomic,strong) NSArray *indicador;
@property (nonatomic, retain) SimpleScatterPlot *scatterPlot;

@end
SimpleScatterPlot.m
中,我想使用数组“
valor
”来配置轴范围,我想使用数组“
strAnoMes
”来在xAxis中绘制自定义标签

我必须做什么才能将ViewController中定义的数组传递给SimpleSchetterPlot


我试图导入“ViewController.h”,但它给了我错误。还有其他想法吗?处理这种情况的最佳方法是什么?

您可以在SimpleSchetterPlot中获取两个属性

SimpleScatterPlot.h

@property (nonatomic, retain) NSArray * strAnoMes;
@property (nonatomic, retain) NSArray * valor;
SimpleScatterPlot.m

@synthesize strAnoMes;
@synthesize valor;
在ViewController.m中,在ConnectionIDFinishLoading:中创建散点图后,按如下所示为上述属性指定值

self.scatterPlot.strAnoMes = strAnoMes;
self.scatterPlot.valor = valor;

一种方法是创建一个简单的数据对象,并将其直接传递到您的
SimpleScatterPlot
对象中:

数据对象:

@interface SimpleScatterPlotData : NSObject
@property (...) NSArray *valor;
@property (...) NSArray *strAnoMes;
@end

@implementaton SimpleScatterPlotData
@synthesize valar;
@synthesize strAnoMes;
-(void)dealloc
{
   ...
   ...
   [super dealloc];
}
@end
SimpleSchetterPlot
类中实现加载方法:

@interface SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data;
@end

@implementation SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data
{
    NSArray *valor = data.valor;
    NSArray *strAnoMes = data.strAnoMes;
    /*
      Do something
    */
}
@end
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    indicador = [NSJSONSerialization JSONObjectWithData:dataConexion options:nil error:nil];

    // ARRAYS OF INTEREST
    strAnoMes = [indicador valueForKey:@"StrAnoMes"];
    valor = [indicador valueForKey:@"Valor"];

    SimpleScatterPlotData *data = [[[SimpleScatterPlotData alloc] init] autorelease];
    data.valor = valor;'
    data.strAnoMes = strAnoMes;

    self.scatterPlot = [[SimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:pointArray];
    [self.scatterPlot loadData:data];
}
然后在
ViewController
类中:

@interface SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data;
@end

@implementation SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data
{
    NSArray *valor = data.valor;
    NSArray *strAnoMes = data.strAnoMes;
    /*
      Do something
    */
}
@end
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    indicador = [NSJSONSerialization JSONObjectWithData:dataConexion options:nil error:nil];

    // ARRAYS OF INTEREST
    strAnoMes = [indicador valueForKey:@"StrAnoMes"];
    valor = [indicador valueForKey:@"Valor"];

    SimpleScatterPlotData *data = [[[SimpleScatterPlotData alloc] init] autorelease];
    data.valor = valor;'
    data.strAnoMes = strAnoMes;

    self.scatterPlot = [[SimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:pointArray];
    [self.scatterPlot loadData:data];
}

您还可以更改当前的初始值设定项或向SimpleSchetterPlot类添加新的初始值设定项,以便在分配对象时传入这些数组。因此,对初始值设定项的调用类似于:

self.scatterPlot = [[SimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:pointArray andValor:valor andstrAnoMes:strAnoMes];

然后在初始值设定项中,可以将对象属性设置为传入的值

我正在按照您的建议进行操作,但是SimpleSchetterPlot中的数组将变为空。您是否在将数组分配给SimpleSchetterPlot时记录了这些值。无需担心!我刚刚把代码放在创建散点图的上面几行。。问题解决了!谢谢