How to access/print an NSMutableArray element from one view to another view?

大憨熊 提交于 2019-12-02 11:01:11

问题


I have an NSMutableArray named mArray in view1 and it stores some strings. Now I want to access/print/compare these elements in view2. Please guide me.


回答1:


write property synthesize for marray in view1 class.Then create view1 object in the view2 and use as view1object.marray




回答2:


Suggestion:

You could put the array in your Controller class,where they both could access.

It is always better to have sharable data in Controller then views if data has to be shared among views.




回答3:


view1.mArray should do it.

Pls note to @synthesize the mArray in view1. To print the array in console

NSLog(@"mArray from view 1 is %@",view1.mArray);



回答4:


If view2 is presented pushed whatever in view1, then you can pass it down as a property from view1.

If both are on the same level, spawned by a super view controller, own the array in a property of the super controller and pass it down to the views.

You can also implement a singleton and share the model as a single instance, accessed directly in the views.




回答5:


I suggest that your views situated in different ViewControllers. There are several kinds:

  1. Create a property for your target viewcontroller and set your array after creating VC instance.
  2. Make -(id)initWithArray:(NSMutableArray*)array; method for your viewcontroller
  3. Use singleton for your whole project,


来源:https://stackoverflow.com/questions/5714586/how-to-access-print-an-nsmutablearray-element-from-one-view-to-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!