Reputation: 283
So I seem to have spent the last couple of days trying to find various solutions for this and have decided it's probably best that I just ask someone. I'm using a storyboard and I send an array from one ViewController to another just fine. But once in this SecondViewController i'm having an issue with it. I would then like to be able to access the array in a subview of this view controller to be able to draw a graph with the data but whenever I try I just always get null from the code in the subview even though it's definitely in the ParentViewController. Could someone please have a look at my current code and let me know how I would get this data in my subview.
ParentViewController.h
#import <UIKit/UIKit.h>
#import "GraphView.h"
#import "Model.h"
@interface GraphViewController : UIViewController
@property (strong) Model *model;
@property (weak) GraphView *graph;
@property (strong) NSArray *data;
@end
ParentViewController.m
#import "GraphViewController.h"
@interface GraphViewController ()
@end
@implementation GraphViewController
@synthesize graph;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"Model data: %@",self.data);//prints just fine
[self.graph setNeedsDisplay];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
SubView.h
#import <UIKit/UIKit.h>
#import "Model.h"
@interface GraphView : UIView
@property (weak) NSArray *array;
@property (strong) Model *model;
@end
Subview.m
#import "GraphView.h"
#import "GraphViewController.h"
@implementation GraphView
- (id)initWithFrame:(CGRect)frame dataArray:(NSArray*)data {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
NSLog(@"Draw Rect");
NSLog(@"%@", self.array);//The bit that keeps returning null
if (self.array != nil){
NSLog(@"Drawing Rect");
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 160, 100);
CGContextAddLineToPoint(ctx,260,300);
CGContextAddLineToPoint(ctx,60,300);
CGContextClosePath(ctx);
[[UIColor blueColor] setFill];
[[UIColor greenColor] setStroke];
CGContextSetLineWidth(ctx,2.0);
CGContextDrawPath(ctx,kCGPathFillStroke);
}
}
Now I know that it would return null now because i'm not actually assigning it to anything but i've tried so many different ways of trying to assign it to the data array in the parent view controller i've forgotten how i even started. Any help would be much appreciated!
Upvotes: 2
Views: 201
Reputation: 1633
There is 3 issues in your code:
Here is my code:
@interface GraphViewController : UIViewController
@property (strong) Model *model;
@property (retain) GraphView *graph; // change to retain
@property (strong) NSArray *data;
@end
@implementation GraphViewController
@synthesize graph;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.data = [[NSArray alloc] initWithObjects:@1, @2, nil];
self.graph = [[GraphView alloc] initWithFrame:CGRectMake(0, 0, 320, 320) dataArray:self.data];
[self.view addSubview:self.graph]; // Init graph and add into subviews.
}
@end
@interface GraphView : UIView
- (id)initWithFrame:(CGRect)frame dataArray:(NSArray*)data;
@property (weak) NSArray *array;
@property (strong) Model *model;
@end
@implementation GraphView
- (id)initWithFrame:(CGRect)frame dataArray:(NSArray*)data {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.array = data; // Assign the array.
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
NSLog(@"Draw Rect");
NSLog(@"%@", self.array);//The bit that keeps returning null
if (self.array != nil){
NSLog(@"Drawing Rect");
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 160, 100);
CGContextAddLineToPoint(ctx,260,300);
CGContextAddLineToPoint(ctx,60,300);
CGContextClosePath(ctx);
[[UIColor blueColor] setFill];
[[UIColor greenColor] setStroke];
CGContextSetLineWidth(ctx,2.0);
CGContextDrawPath(ctx,kCGPathFillStroke);
}
}
@end
Upvotes: 0
Reputation: 39988
Assign GraphView
's data in viewDidLoad
. Also make sure that your self.graph
is properly connected/initialized.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"Model data: %@",self.data);//prints just fine
self.graph.array = self.data;
[self.graph setNeedsDisplay];
}
Upvotes: 1