سؤال

I have an app with a mapview containing various building objects (my own class). Each building has an annotation property which all works great and the user can search a table for the desired building, tap a button and it'll show the relevant annotation.

Now I have a callout button on the annotation which calls a method (showDetails) that pushes a segue to the DetailViewController, but the issue I'm having is getting the right building across to this DetailViewController. All the buildings are stored in an array in the data controller and I'd like to load one from that if possible.

So far I've used the controlWasTapped method where I've simply put:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

for (Building* theBuilding in self.datacontroller.masterArray){
if(theBuilding.name isEqualToString:view.annotation.title){
    NSLog(@"theBuilding's name %@", theBuilding.name);
}
}

This works fine and returns the correct building but I'm stumped as to how to get this to the DetailViewController. I have a PrepareForSegue method but how would I get the correct building to it? Also, now that I have DidSelectAnnotation, is there any need for my showDetails method?

I was thinking I could modify the showDetails method so that it took an argument of Building type and then I could provide the Building details in prepareForSegue but if there is a much better way I'd love to know.

So, what's the best plan? Many thanks

هل كانت مفيدة؟

المحلول

Declare an instance variable in the class that contains the code you posted.

Building *buildingOfInterest;

then

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

    for (Building* theBuilding in self.datacontroller.masterArray)
        if(theBuilding.name isEqualToString:view.annotation.title) {
              buildingOfInterest = theBuilding;
              [self performSegueWithIdentifier:@"ShowBuildingSegue" sender:self];
              break;
    }
}

then:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([[segue identifier] isEqualToString:@"ShowBuildingSegue"])
         ((MyBuildingVC *)segue.destinationViewController).building = buildingOfInterest;
}

and in your target view controller's header file:

@interface MyBuildingVC : UIViewController
@property (strong) Building * building;
@end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top