I have been poking around with the MKMapView for quite some time trying to get more familiar with it and I ran into a problem.
I have my MapView populated with two pins and when I pressed them they have their respective annotations. I also have a button that will take me to a new UIView with UlLabels that will be loaded with data from an array.
Looking at my console I notice that the data is coming through but instead of it being just for the pin I selected its loading all of them and then displaying the last one.
Here is my code for the method I’m using to load the data for the next view:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:addAnnotation reuseIdentifier:@"currentloc"];
if(annView.tag = 0) {
GSStore * theStore = [globalCMS getStoreById:1];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int storeid = theStore.storeid;
[prefs setInteger:storeid forKey:@"selectedstore"];
NSLog(@"%@", theStore.name);
storeName.text = [NSString stringWithFormat:@"%@", theStore.name];
storeCS.text = [NSString stringWithFormat:@"%@,%@", theStore.city, theStore.state];
storeHours.text = [NSString stringWithFormat:@"%@", theStore.hours];
storePhone.text = [NSString stringWithFormat:@"%@", theStore.phone];
storeAddress.text = [NSString stringWithFormat:@"%@", theStore.address];
storeCSZ.text = [NSString stringWithFormat:@"%@,%@ %@", theStore.city, theStore.state, theStore.zip];
storeWebsite.text = [NSString stringWithFormat:@"%@", theStore.website];
}
if(annView.tag = 1){
GSStore * theStore = [globalCMS getStoreById:2];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int storeid = theStore.storeid;
[prefs setInteger:storeid forKey:@"selectedstore"];
NSLog(@"%@", theStore.name);
storeName.text = [NSString stringWithFormat:@"%@", theStore.name];
storeCS.text = [NSString stringWithFormat:@"%@,%@", theStore.city, theStore.state];
storeHours.text = [NSString stringWithFormat:@"%@", theStore.hours];
storePhone.text = [NSString stringWithFormat:@"%@", theStore.phone];
storeAddress.text = [NSString stringWithFormat:@"%@", theStore.address];
storeCSZ.text = [NSString stringWithFormat:@"%@,%@ %@", theStore.city, theStore.state, theStore.zip];
storeWebsite.text = [NSString stringWithFormat:@"%@", theStore.website];
}
if (annView.tag = 2) {
GSStore * theStore = [globalCMS getStoreById:3];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int storeid = theStore.storeid;
[prefs setInteger:storeid forKey:@"selectedstore"];
NSLog(@"%@", theStore.name);
storeName.text = [NSString stringWithFormat:@"%@", theStore.name];
storeCS.text = [NSString stringWithFormat:@"%@,%@", theStore.city, theStore.state];
storeHours.text = [NSString stringWithFormat:@"%@", theStore.hours];
storePhone.text = [NSString stringWithFormat:@"%@", theStore.phone];
storeAddress.text = [NSString stringWithFormat:@"%@", theStore.address];
storeCSZ.text = [NSString stringWithFormat:@"%@,%@ %@", theStore.city, theStore.state, theStore.zip];
storeWebsite.text = [NSString stringWithFormat:@"%@", theStore.website];
}
}
Just to explain why you are seeing “the method loading all the data”:
The
ifconditions are using a single equals (=) sign instead of a double (==).The single
=is an assignment while the double==is the one for checking equality.Since the assignment executes successfully in all the
ifs, the code inside all theifs executes.However, the real problem is that you are creating a new instance of an annotation view in the
didSelectAnnotationViewdelegate method which is not what you want.Instead of creating a new instance, you can use the annotation view instance that the method is providing you as a parameter (ie.
view).So theoretically you could look at
view.tag.But I would highly recommend not relying on tags and instead placing all the data you need for an annotation in the annotation class itself. Then you can access the annotation in this method using
view.annotationand casting it to your custom class to access custom properties: