MKMapView show DetailView - How to make a segue

a 夏天 提交于 2019-12-24 19:25:28

问题


I have a MapView, the annotations comes from a property list. Now I would like to see more data in the detail view. Unfortunately I do not how to program the Segue, so that data can be displayed. I hope you understand what I mean. My english is not so good ... I know that there is missing some in the method prepareforSegue. I am using storyboard. In advance thank you for your help ... regards

#import "MapViewNewController.h"
#import "MapViewAnnotation.h"
#import "SetCardController.h"



@interface MapViewNewController ()



@end

@implementation MapViewNewController 


@synthesize recipesDictionary = _recipesDictionary;

@synthesize mapView;
@synthesize locationManager;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.delegate = self;

    [self.mapView setShowsUserLocation:YES];


    MKCoordinateRegion newRegion ;
    newRegion.center.latitude = 50.080635;
    newRegion.center.longitude = 8.518717;
    newRegion.span.latitudeDelta = 15.5;
    newRegion.span.longitudeDelta = 15.5;
    [mapView setRegion:newRegion animated:YES];



    NSString *path = [[NSBundle mainBundle] pathForResource:@"Rezepte" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *anns = [dict objectForKey:@"myRecipes"];
    for(int i = 0; i < [anns count]; i++) {
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"longitude"] floatValue];

        MapViewAnnotation *myAnnotation = [[MapViewAnnotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"blogname"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"blogger"];
        [mapView addAnnotation:myAnnotation];


    }      

}


-(MKAnnotationView *)mapView:(MKMapView *)view viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *pin = nil;

    if ([annotation isKindOfClass:[MapViewAnnotation class]]) {
        NSString *pinIdentifier = @"myPin";


        pin = (MKPinAnnotationView*)[view dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
        if (!pin) {


            pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];

            pin.image = [UIImage imageNamed:@"pin2.png"];
            pin.canShowCallout = YES;

            pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


        }
            }

    return pin;
}



//if Button pressed
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

     [self performSegueWithIdentifier:@"showPinDetails" sender:self];




    }

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"showPinDetails"]) {

       // SetCardController *scc = [segue destinationViewController];


    }
}

回答1:


When you performSegueWithIdentifier:sender from a callout inside annotationView:view, the sender can be the view.annotation property, which uniquely identifies the callout that the user just tapped.




回答2:


I believe you didn't set the identifier for the Segue in Interface Builder. I tried your code and it worked for me (Xcode 4.4 iOS 5.1 SDK)




回答3:


The above two answers help, however don't help you identify the unique pins details that I am assuming are part of your p-list.

All Views inherit a the property of a Tag. It can used this to hold a pin index (or key)and therefore pass this to the destination View Controller to uniquely identify the data you hold.

As your button in the call-out view inherits from view you can tag it, and pass the index when its active in the method didselectAnnotationView. The if statement used de-selects the User location pin.

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

if (![(PointAnnotation *)view.annotation isKindOfClass:[MKUserLocation class]])
{
    PointAnnotation *myAnn = (PointAnnotation *)view.annotation;

    detailButton.tag = myAnn.pinIndex;
    NSLog (@"Pinindex = %d", myAnn.pinIndex);
}



}

The detailButton.tag can now be used as a parameter in segue selection.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Post data to destination VC

if ([[segue identifier] isEqualToString:@"clubsToDetail"])
{


    ClubsMasterData *current = [[ClubsMasterxmlParser ClubsMasterDatas] objectAtIndex:detailButton.tag];


    ClubsDetailViewController *DVC = [[ClubsDetailViewController alloc] init];
    DVC = [segue destinationViewController];


    DVC.linkURL =  current.linkURL;
    DVC.distance = current.distance;

}
}

Here I indexed an Array rather than a Plist, but the principal is the same.



来源:https://stackoverflow.com/questions/11741334/mkmapview-show-detailview-how-to-make-a-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!