0

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

视图控制器.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”在xAxis中绘制自定义标签。

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

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

4

3 回答 3

1

您可以在 SimpleScatterPlot 中获取两个属性。

简单散点图.h

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

简单散点图.m

@synthesize strAnoMes;
@synthesize valor;

在 ViewController.m 中,在 connectionDidFinishLoading: 中创建散点图后,将值分配给上述属性,如下所示。

self.scatterPlot.strAnoMes = strAnoMes;
self.scatterPlot.valor = valor;
于 2013-02-25T18:09:21.617 回答
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];
}
于 2013-02-25T18:34:36.757 回答
0

您还可以更改当前初始化程序或将新初始化程序添加到 SimpleScatterPlot 类,以便在分配对象时传递这些数组。因此,您对初始化程序的调用将类似于:

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

Then in your initializer you can set the object properties to those passed in values.

于 2013-02-25T19:02:26.537 回答