所以我似乎花了最近几天试图为此找到各种解决方案,并且已经决定我最好只问一个人。 我正在使用故事板,我从一个ViewController发送数组到另一个就好了。但是一旦在这个SecondViewController中我遇到了问题。然后,我希望能够在该视图控制器的子视图中访问该数组,以便能够使用数据绘制图形,但每当我尝试时,我总是从子视图中的代码中获取null,即使它是肯定在ParentViewController中。有人可以看看我目前的代码,让我知道如何在我的子视图中获取这些数据。
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);
}
}
现在我知道它现在会返回null,因为我实际上并没有将它分配给任何东西,但是我已经尝试了很多不同的方法来尝试将它分配给父视图控制器中的数据数组i&忘记了我是怎么开始的。任何帮助将不胜感激!
答案 0 :(得分:1)
在GraphView
中分配viewDidLoad
个数据。另外,请确保您的self.graph
已正确连接/初始化。
- (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];
}
答案 1 :(得分:0)
您的代码中有3个问题:
这是我的代码:
@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