ios - Store annotation on map using state restoration -
i'm adding annotation @ user position in viewdidload , try store annotation using state restoration. want annotation stay there if app gets terminated in background.
-(void)viewdidload { ... self.annotationregion = mkcoordinateregionmake(cllocationcoordinate2dmake(self.mapview.userlocation.location.coordinate.latitude, self.mapview.userlocation.location.coordinate.longitude), mkcoordinatespanmake(0.5, 0.5)); self.parkinglocation = [[swannotation alloc] init]; self.parkinglocation.title = @"here is!"; self.parkinglocation.coordinate = self.annotationregion.center; [self.mapview addannotation:self.parkinglocation]; nslog(@"userlocation: %@", self.mapview.userlocation); nsnumber *longitudenumber = [nsnumber numberwithdouble:self.annotationregion.center.longitude]; nsnumber *latitudenumber = [nsnumber numberwithdouble:self.annotationregion.center.latitude]; self.coordinatearray = @[latitudenumber,longitudenumber]; } -(void)encoderestorablestatewithcoder:(nscoder *)coder { ... [coder encodeobject:self.coordinatearray forkey:@"coordinatearray"]; } -(void)decoderestorablestatewithcoder:(nscoder *)coder { ... nsarray *coordinatearray = [coder decodeobjectforkey:@"coordinatearray"]; self.annotationregion = mkcoordinateregionmake(cllocationcoordinate2dmake([coordinatearray[0] doublevalue], [coordinatearray[1] doublevalue]), mkcoordinatespanmake(1.5,1.5)); self.parkinglocation = [[swannotation alloc] init]; self.parkinglocation.title = @"tata!"; self.parkinglocation.coordinate = self.annotationregion.center; [self.mapview addannotation:self.parkinglocation]; }
note, in initial viewdidload annotation use title "here is!" , in decoderestorablestatewithcoder annotation use title "tata!" see 1 it's showing me @ moment.
now, seems work when app gets terminated first time in background. app starts , has annotation new title "tata!" @ right position. second time gets terminated in background , gets started shows me annotation initial title of "here is!" off coast of africa, of course not position... sooo i'm getting frustrated, missing?
it seems me when encode state encode self.coordinatearray
, when restore use local variable nsarray *coordinatearray
restore. when app encode again later, self.coordinatearray
nil.
i think should set [self setcoordinatearray: coordinatearray];
after nsarray *coordinatearray = [coder decodeobjectforkey:@"coordinatearray"];
Comments
Post a Comment