MKMapView显示DetailView - 如何制作segue

时间:2012-07-31 13:34:12

标签: ios mkmapview uistoryboard uistoryboardsegue

我有一个MapView,注释来自属性列表。 现在我想在详细视图中看到更多数据。 不幸的是我不知道如何编程Segue,因此可以显示数据。 我希望你明白我的意思。我的英语不怎么好 ..​​. 我知道方法prepareforSegue中缺少一些。 我正在使用故事板。 提前谢谢你的帮助...... 问候

#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];


    }
}

3 个答案:

答案 0 :(得分:1)

当您performSegueWithIdentifier:sender内的标注annotationView:view时,发件人可以是view.annotation属性,该属性唯一标识用户刚刚点按的标注。

答案 1 :(得分:0)

我相信您没有在Interface Builder中设置Segue的标识符。我尝试了你的代码,它适用于我(Xcode 4.4 iOS 5.1 SDK)

答案 2 :(得分:0)

以上两个答案有帮助,但不能帮助您确定我假设的唯一引脚详细信息是您的p列表的一部分。

所有视图都继承了Tag的属性。它可用于保存引脚索引(或键),因此将其传递到目标视图控制器以唯一标识您保存的数据。

当调出视图中的按钮继承自视图时,您可以对其进行标记,并在方法didselectAnnotationView中激活时传递索引。使用的if语句取消选择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);
}



}

现在可以将detailButton.tag用作segue选择中的参数。

-(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;

}
}

这里我索引了一个数组而不是一个Plist,但是主体是相同的。