【问题标题】:MKMapView show DetailView - How to make a segueMKMapView 显示 DetailView - 如何进行转场
【发布时间】:2012-07-31 13:34:12
【问题描述】:

我有一个 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];


    }
}

【问题讨论】:

    标签: ios mkmapview uistoryboard uistoryboardsegue


    【解决方案1】:

    当您从annotationView:view 内部的标注中performSegueWithIdentifier:sender 时,发送者可以是view.annotation 属性,它唯一标识用户刚刚点击的标注。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        以上两个答案有帮助,但不能帮助您识别我假设是您的 p-list 的一部分的唯一引脚详细信息。

        所有视图都继承了标签的属性。它可以使用它来保存 pin 索引(或键),因此将它传递给目标 View Controller 以唯一标识您持有的数据。

        由于调用视图中的按钮继承自视图,因此您可以标记它,并在它处于活动状态时在方法 didselectAnnotationView 中传递索引。使用的 if 语句取消选择用户位置引脚。

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

        这里我索引的是 Array 而不是 Plist,但原理是一样的。

        【讨论】:

          猜你喜欢
          • 2011-12-12
          • 2011-10-01
          • 2012-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-23
          • 1970-01-01
          • 2012-06-07
          相关资源
          最近更新 更多