ios viewcontroller视图图与可变数组一起使用 [英] ios viewcontroller view graph live with Mutable Array

查看:45
本文介绍了ios viewcontroller视图图与可变数组一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我想要实现的是在视图中绘制图形,同时通过Array将数据实时添加到VC中.

到目前为止,我有一个VC和一个带有自己类的视图.在类内部,在drawRect内部,我生成随机数,它们在屏幕上显示为点.到目前为止,一切都很好.

我想做的是在ViewController中生成数字(或通过外部源获取它们),将它们保存在mutableArray中,在类内检索它们并将其用于图形.

从2年前开始,我在SO上也发现了类似的问题,因此对此没有任何评论.问题是:我在FirstViewController上有一个带浮点值的数组.我需要传递给查看此数组"

被标记为有效的推荐解决方案是:

在您的FirstViewController.h中写

  #include< UIKit/UIKit.h>extern NSArray * yourArray; 

然后在您的FirstViewController.m中写

  #import"FirstViewController.h"@interface FirstViewController()@结尾NSArray * yourArray; 

现在,您必须将整个FirstViewController导入"MyView"类中.只需在MyView.m中#import"FirstViewController.h".这样做之后,"yourArray"将在MyView中可用.

我完全按照它的意思去做,然后初始化我的数组,在其中放入值,在VC中使用NSLog来检查所有工作是否正常,并且这样做了.当我尝试在视图中检索一个值并将其记录到NSLog时,它说数组中的值为(null).代码是如此简单,几乎没有什么可显示的:

在我的VC.h中

  extern NSMutableArray * yourArray; 

在我的VC.m实现中

  NSMutableArray * yourArray; 

在我的VC.m中viewDidLoad

  NSMutableArray * yourArray = [[NSMutableArray alloc] init];[yourArray insertObject:@"Works" atIndex:0];NSString * object = [yourArray objectAtIndex:0];NSLog(@"VC中Array中的项目为%@",对象); 

在我看来.m

  #import"GraphViewController.h" 

然后我尝试添加:

  NSString * object = [yourArray objectAtIndex:0];NSLog(@"View中的Array中的项目为%@",对象); 

在(void)awakeFromNib,在(id)initWithFrame:(CGRect)框架,在(void)updateValues和(void)drawRect:(CGRect)rect中

在我放置的任何地方都无法使用.我完全不知所措.

有人可以指出正确的方向吗?

谢谢

============================================

解决方案

我将尝试一点一点地解决这个问题.

您有:UIViewController的子类,称为GraphViewController.UIView的子类,称为MyGraphView.一个故事板.

在情节提要中,您有一个UIViewController场景,其中包含一个UIView对象.在身份检查器"中,将UIViewController场景的自定义类"字段设置为GraphViewController.正确的?同样在Identity Inspector中,将UIView对象自定义类"字段设置为MyGraphView.正确吗?

因此,当您从View对象右键单击并拖动到GraphViewController的头文件时,弹出窗口似乎准备就绪,可供您输入属性名称,并且该弹出窗口的类型"字段已设置为MyGraphView.正确吗?

设置了IBOutlet属性之后,您可以在情节提要中选择GraphViewController并显示Connections检查器,并查看是否存在一个连接到GraphViewController的名为MyGraphView的引用插座.

因此,如果所有这些都成立,那么将在情节提要中设置您的viewController和View.

GraphViewController具有一个称为NSMutableArray的属性,称为yourArray.GraphViewController具有一个名为MyView的MyGraphView属性.MyGraphView具有一个称为graphDataArray的NSArray属性.

因此,在您的GraphViewController中,将此方法粘贴到-viewDidLoad上方.

 -(void)viewDidAppear:(BOOL)动画{[super viewDidAppear:animated];self.yourArray = [@ [@"Testing"] mutableCopy];如果(self.myView == nil){NSLog(@名为MyView的MyGraphView实例不存在");返回;}别的{NSLog(@名为MyView的MyGraphView实例存在%@",self.myView);}self.myView.graphDataArray = [self.yourArray复制];如果(self.myView.graphDataArray == nil){NSLog(@"GraphDataArray为零");}别的 {NSLog(@索引为0的对象=%@",self.myView.graphDataArray [0]);NSLog(@"MyGraphView现在可以从GraphViewController接收数组了:]");}} 

Good morning,

what I try to achieve is to draw a graph inside a view whilst data are being added live in the VC through an Array.

So far I have a VC and a view with its own class. Inside the class, inside drawRect I generate random numbers and they appear on the screen as dots. So far, so good.

What I want to do is generate the numbers in the ViewController (or get them via an external source), save them in a mutableArray, retrieve them inside the class and use them for the graph.

I found a similar question here on SO, from 2 years ago so there is no point commenting on it. The question was : I have on FirstViewController an array with float value. I need to pass to my View this array

The recommended solution, that was marked as working was :

In your FirstViewController.h write

#include <UIKit/UIKit.h>
extern NSArray *yourArray;

And in your FirstViewController.m write

#import "FirstViewController.h"
@interface FirstViewController ()
@end
NSArray *yourArray;

Now you'll have to import the whole FirstViewController in your "MyView"-Class. Simply #import "FirstViewController.h" in MyView.m. After doing so, "yourArray" will be available in MyView.

I do exactly what it says, then I initialize my Array, put values in it, NSLog in VC to check all working fine and it does. When I try to retrieve one value in my view and NSLog it, it says the value in the array is (null). The code is so simple I almost have nothing to show:

In my VC.h

extern NSMutableArray *yourArray;

In my VC.m implementation

NSMutableArray *yourArray;

In my VC.m viewDidLoad

NSMutableArray *yourArray = [[NSMutableArray alloc] init];
[yourArray insertObject:@"Works" atIndex:0];
NSString *object = [yourArray objectAtIndex:0];
NSLog (@"the item in Array in VC is %@", object);

In my view.m

#import "GraphViewController.h"

And then I tried adding:

NSString *object = [yourArray objectAtIndex:0];
NSLog (@"the item in Array in View is %@", object);

in (void)awakeFromNib, in (id)initWithFrame:(CGRect)frame, in (void)updateValues and in (void)drawRect:(CGRect)rect

It doesn't work from anywhere I put it. I am at a complete loss.

Could somebody please point me in the right direction ?

Thanks,

=============================================

解决方案

I'm gonna try and go through this bit by bit.

You have: A subclass of UIViewController called GraphViewController. A subclass of UIView called MyGraphView. A storyboard.

In your storyboard you have a UIViewController scene which contains a UIView object. In the "Identity Inspector" you have set the UIViewController scene "Custom Class" field to GraphViewController. Correct? Also in the Identity Inspector you have set the UIView object Custom Class field to MyGraphView. Correct?

So when you right-click-drag from the View object to the header file of GraphViewController the pop up appears ready for you to input a property name and the "Type" field of that popup is already set to be MyGraphView. Correct?

After the IBOutlet property has been set you can select the GraphViewController in storyboard and show the Connections Inspector and see that there exists a referencing Outlet called MyGraphView connected to GraphViewController.?

So if all those are true then your viewController and View are set up in storyboard.

GraphViewController has a property which is an NSMutableArray called yourArray. GraphViewController has a property which is an MyGraphView called myView. MyGraphView has a property which is an NSArray called graphDataArray.

So in your GraphViewController paste this method above -viewDidLoad.

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.yourArray = [@[@"Testing"] mutableCopy];
    if (self.myView == nil) {
        NSLog(@"The Instance of MyGraphView called MyView does not exist");
        return;
    }else{
        NSLog(@"The Instance of MyGraphView called MyView exists %@",self.myView);
    }
    self.myView.graphDataArray = [self.yourArray copy];
    if (self.myView.graphDataArray == nil) {
        NSLog(@"GraphDataArray is nil");
    }else {
        NSLog(@"Object at index 0 = %@",self.myView.graphDataArray[0]);
        NSLog(@"MyGraphView can now receive arrays from GraphViewController :]");
    }
}

这篇关于ios viewcontroller视图图与可变数组一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆