将数组从一个视图添加到另一个视图并保留值 [英] Adding array from one view to another and retaining values

查看:90
本文介绍了将数组从一个视图添加到另一个视图并保留值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个视图和四个数组。从一个视图导航后,便将该特定数组添加到我的主数组中。

I have four views, with four arrays. As soon as I navigate from one view, I add that particular array to my master array.

最初,该主数组没有任何内容,并且在应用程序使用 init方法运行时被分配。它没有视图,它是来自 NSObject 的子类的数组。

Initially this master array has nothing, and is allocated when the app runs using 'init' method. It does not have a view and it is an array from a subclass of NSObject.

最后,主数组应由{viewarray1 ,viewarray2,viewarray3,viewarray4}。

Finally the master array should consist of {viewarray1, viewarray2, viewarray3, viewarray4}.

在导航到下一个视图时,每个数组都会添加到主数组中。

Each array is added to the master array when navigating to the next view.

所以有什么问题吗?在我的实施中?在 init 方法中分配 masterArray 可以吗?每次将对象添加到 masterArray 时,我 NSLog 并显示(空)

So is there anything wrong in my implementation ? Is it okay to allocate masterArray in the init method? Each time I add an object to masterArray, I NSLog it and it displays (null)

我如何让主阵列保留整个应用程序的值?

How can I have the master array retain values for the whole app??

有关某些信息代码

我使用-(init)方法

I initialize my master array in another class, in the -(init) method

masterArray = [[NSMutableArray alloc] init ];

从另一个视图向MasterArray添加对象时,我引用了该类,并为该类创建了一个对象,并且将其添加为属性合成大小。然后,我使用

While adding an object to MasterArray from another view, I reference that class, and I create an object for the class, and add it as a property and synthesize it. I then use

[self.thatClassObject.masterArray addObject:self.viewArray1];


推荐答案

有两种方法可以对其进行初始化:我可以想一想。首先,您可以在应用程序委托中分配/初始化数组。

There are two ways you could go about initializing it that I can think of offhand. First, you could alloc/init the array in the app delegate. This will ensure it's created before the rest of the views get a chance to add to it.

编辑:乔什·卡斯威尔(Josh Caswell)是我想到的唯一一种方法指出了一个很好的事实,类初始化不适用于这种情况。最好不要在应用程序委托中调用数组的alloc / init,或者先将哪个视图设为键窗口。您必须从那里参考其他课程

There's only really one way I can think to do this as Josh Caswell pointed out a good fact that class initializations won't work for this situation. You're best off calling alloc/init for the array either in the app delegate or whichever view is made key window first. You'll have to reference it to the other classes from there

这篇关于将数组从一个视图添加到另一个视图并保留值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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