将NSArrays从ViewController传递给NSObject类

时间:2013-02-25 17:53:37

标签: objective-c cocoa-touch nsarray core-plot

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

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

在ViewController.m中,我有一些方法可以从一些JSON数据初始化一些数组:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize indicador,valor,strAnoMes;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSString *urlString = @"http://xxxx/ios/datosgrafica.php";

    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];   
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    dataConexion = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [dataConexion appendData:theData];
}

- (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"];

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

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

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

我试过#import "ViewController.h",但它给了我错误。还有其他想法吗?处理这种情况的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

您可以在SimpleScatterPlot中使用两个属性。

SimpleScatterPlot.h

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

SimpleScatterPlot.m

@synthesize strAnoMes;
@synthesize valor;

在ViewController.m中,在connectionDidFinishLoading:中创建scatterPlot后,将值分配给上面的属性,如下所示。

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

答案 1 :(得分:1)

执行此操作的一种方法是创建一个简单的数据对象并将其直接传递到SimpleScatterPlot对象中:

数据对象:

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

@implementaton SimpleScatterPlotData
@synthesize valar;
@synthesize strAnoMes;
-(void)dealloc
{
   ...
   ...
   [super dealloc];
}
@end

SimpleScatterPlot班级中实施加载方法:

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

@implementation SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data
{
    NSArray *valor = data.valor;
    NSArray *strAnoMes = data.strAnoMes;
    /*
      Do something
    */
}
@end

然后在你的ViewController课程中:

- (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];
}

答案 2 :(得分:0)

您还可以更改当前的初始化程序或向SimpleScatterPlot类添加新的初始化程序,以便在分配对象时传递这些数组。所以你对初始化程序的调用看起来像是:

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

然后在初始化程序中,您可以将对象属性设置为值传递的属性。