从子视图中的父视图控制器检索数组

Retrieve an array from a Parent View Controller in a subview

因此,过去几天我似乎一直在尝试为此寻找各种解决方案,并决定最好还是问问别人。 我正在使用故事板,我将一个数组从一个 ViewController 发送到另一个就好了。但是在这个 SecondViewController 中,我遇到了一个问题。然后我希望能够访问此视图控制器的子视图中的数组,以便能够使用数据绘制图形,但每当我尝试时,我总是从子视图中的代码中获取 null,即使它肯定在父视图中ViewController。有人可以看看我当前的代码,让我知道如何在我的子视图中获取这些数据。

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);
    }

 }

现在我知道它现在 return null 因为我实际上并没有将它分配给任何东西,但我已经尝试了很多不同的方法来尝试将它分配给父视图中的数据数组控制器我什至忘记了我是如何开始的。任何帮助将不胜感激!

viewDidLoad中分配GraphView的数据。还要确保您的 self.graph 正确 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];
}

您的代码中存在 3 个问题:

  1. @属性(弱)GraphView *graph; // 图很弱,对象会 分配后释放。
  2. GraphView 中 self.array 没有分配。
  3. 未将图表添加到 controller.view.subviews。

这是我的代码:

@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