Reputation: 433
I have a single view application where the ViewController contains a UIView of custom class CPTGraphHostingView
so that it holds a CorePlot graph. The content of that UIView is managed by an NSObject sub-class called SimpleScatterPlot
. This SimpleScatterPlot
object holds all the parameters needed to configure the graph (axis ranges, labels, number of points, etc.)
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
In ViewController.m I have some methods to initialise a few arrays from some JSON data:
#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;
}
In the SimpleScatterPlot.m
I would like to use the array "valor
" to configure axis ranges, and I would like to use the array "strAnoMes
" to draw custom labels in the xAxis.
What do I have to do so that I can pass those arrays defined in ViewController to SimpleScatterPlot?
I have tried to #import "ViewController.h"
but it gives me errors. Any other ideas? What is the best way to approach this situation?
Upvotes: 0
Views: 839
Reputation: 61
You could also change your current initializer or add a new initializer to your SimpleScatterPlot class so that you pass those arrays in when you alloc the object. So your the call to your initializer would look something like:
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.
Upvotes: 0
Reputation: 9030
One way to do this is to create a simple data object and pass it directly into your SimpleScatterPlot
object:
Data Object:
@interface SimpleScatterPlotData : NSObject
@property (...) NSArray *valor;
@property (...) NSArray *strAnoMes;
@end
@implementaton SimpleScatterPlotData
@synthesize valar;
@synthesize strAnoMes;
-(void)dealloc
{
...
...
[super dealloc];
}
@end
Implement a load method in your SimpleScatterPlot
class:
@interface SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data;
@end
@implementation SimpleScatterPlot
-(void)loadData:(SimpleScatterPlotData *)data
{
NSArray *valor = data.valor;
NSArray *strAnoMes = data.strAnoMes;
/*
Do something
*/
}
@end
Then in your ViewController
class:
- (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];
}
Upvotes: 1
Reputation: 1229
You can take two properties in SimpleScatterPlot.
SimpleScatterPlot.h
@property (nonatomic, retain) NSArray * strAnoMes;
@property (nonatomic, retain) NSArray * valor;
SimpleScatterPlot.m
@synthesize strAnoMes;
@synthesize valor;
In ViewController.m, After creating scatterPlot in connectionDidFinishLoading:, assign the values to above properties as below.
self.scatterPlot.strAnoMes = strAnoMes;
self.scatterPlot.valor = valor;
Upvotes: 1