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

Ios 从子视图中的父视图控制器检索数组,ios,objective-c,arrays,uiview,uiviewcontroller,Ios,Objective C,Arrays,Uiview,Uiviewcontroller,所以,我似乎花了过去几天的时间试图找到各种解决方案,并决定最好还是问问别人。 我使用的是故事板,我将一个数组从一个ViewController发送到另一个ViewController。但有一次,我觉得有点问题。然后,我希望能够访问此视图控制器子视图中的数组,以便能够使用数据绘制图形,但无论何时尝试,我总是从子视图中的代码中获取null,即使它肯定在ParentViewController中。请有人看一下我当前的代码,让我知道如何在我的子视图中获取这些数据 ParentViewController

所以,我似乎花了过去几天的时间试图找到各种解决方案,并决定最好还是问问别人。 我使用的是故事板,我将一个数组从一个ViewController发送到另一个ViewController。但有一次,我觉得有点问题。然后,我希望能够访问此视图控制器子视图中的数组,以便能够使用数据绘制图形,但无论何时尝试,我总是从子视图中的代码中获取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
 #import <UIKit/UIKit.h>
 #import "Model.h"
 @interface GraphView : UIView 
 @property (weak) NSArray *array;
 @property (strong) Model *model;
 @end
子视图.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
 #import <UIKit/UIKit.h>
 #import "Model.h"
 @interface GraphView : UIView 
 @property (weak) NSArray *array;
 @property (strong) Model *model;
 @end

现在我知道它将返回null,因为我实际上没有将它分配给任何对象,但是我尝试了很多不同的方法,试图将它分配给父视图控制器中的数据数组,我甚至忘记了我是如何开始的。任何帮助都将不胜感激

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

代码中有3个问题:

  • @属性(弱)GraphView*graph;//图形弱,对象将 任务结束后释放
  • GraphView中的self.array没有赋值
  • 未将图形添加到controller.view.subview中
  • 这是我的密码:

    @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
    

    您在
    ParentViewController
    中的何处设置视图?另外-据我所知,您不需要在父级中显示
    setNeedsDisplay
    。视图是使用界面生成器设置的,然后设置为类。然后使用@property(弱)GraphView*图进行引用;这种设置方式适用于我的其他视图,尽管我的应用程序中的这些视图不需要向其发送数组。对不起,这也不起作用,self.graph.data不存在,如果您是指self.graph.array,它也将返回null。我编辑了我的答案,您还可以确认您的
    self.graph
    是否为
    non-nil
    ,因为我看不到它是如何分配的(code/xib)哦,看来我的视图返回了nil,我已经通过界面生成器设置了它,我是如何为其他视图设置的,我不太清楚为什么这个视图返回nil,而我的其他视图返回nil为什么我问你,因为它不是
    IBOutlet
    ,因此它没有连接到xib。请确保它是
    非nil
    明白了,我完全不知道为什么,但出于某种原因,在将它连接到我的故事板时,我需要将它设置为IBOutlet。但是,是的,你的代码确实有效,谢谢!