我有一个单视图应用程序,其中ViewController包含自定义类CPTGraphHostingView
的UIView,以便它包含CorePlot图形。该UIView的内容由名为SimpleScatterPlot
的NSObject子类管理。此SimpleScatterPlot
对象保存将NSArrays从ViewController传递到NSObject类
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
”绘制立方米在xAxis中使用stom标签。
我该怎么做才能将ViewController中定义的数组传递给SimpleScatterPlot?
我试过#import "ViewController.h"
但它给了我错误。任何其他想法?处理这种情况的最佳方式是什么?
我正在做你所建议的,但SimpleScatterPlot中的数组是空的。 – 2013-02-25 19:44:05
您是否在将数组分配给SimpleScatterPlot时记录了值。 – nkongara 2013-02-25 19:49:59
没关系!我刚刚将代码放在scatterPlot的创建之上几行。问题解决了!谢谢! – 2013-02-25 19:54:48