问题
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:
- 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,
来源:https://stackoverflow.com/questions/5714586/how-to-access-print-an-nsmutablearray-element-from-one-view-to-another-view