Adding array from one view to another and retainin

2019-08-19 07:22发布

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

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.

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.

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??

Some Information on the Code:

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

masterArray = [[NSMutableArray alloc] init ];

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];

1条回答
该账号已被封号
2楼-- · 2019-08-19 07:44

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.

EDIT: 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

查看更多
登录 后发表回答