objective c - Remove array elements issue in iOS? -
i have strange issue in ios.i have 2 classes aviewcontroller & bviewcontroller. in aviewcontroller having array
@property nsmutablearray *array_one;
i passing data 1 view controller in prepareforsegue method.
if([self.array_one count]>0) { viewbcontroller.array_two=self.array_one; }
then bviewcontroller passing data this
-(void)viewwilldisappear:(bool)animated { int j; [self.delegate setdata:self.array_two]; }
then in method doing work
- (void)setdata:(nsmutablearray *)data { nsstring *string=[nsstring stringwithformat:@"%lu",(long)[data count]]; [self.array_one removeallobjects]; int i; for(i=0;i<[string intvalue];i++) { user *u=[data objectatindex:i]; [self.array_one addobject:u]; } }
now happened if go view 1 controller 2 times in second time if add line [self.array_one removeallobjects];
data
array empty.i don't know why? please guide me
you use 2 different methods of passing data.
viewbcontroller.array_two=self.array_one;
this assigns array_two
pointer same array_one
. means both point same array.
then, when call [self.delegate setdata:self.array_two]
, first thing happens call [self.array_one removeallobjects]
. pointing same array array_two
, empty array.
if data manifestly same @ times between 2 classes, 1 solution assign array both view controllers once, after changes in 1 reflected in other.
if not, need copy array new object when assign second view controller.
Comments
Post a Comment