How to access/print an NSMutableArray element from one view to another view?
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.
write property synthesize for marray in view1 class.Then create view1 object in the view2 and use as view1object.marray
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.
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);
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.
I suggest that your views situated in different ViewControllers
. There are several kinds:
- Create a property for your target viewcontroller and set your array after creating
VC
instance. - Make
-(id)initWithArray:(NSMutableArray*)array;
method for your viewcontroller - Use singleton for your whole project,
精彩评论