【问题标题】:Prepare to segue when calloutAccessoryControlTapped (get data from parse.com)准备在 calloutAccessoryControlTapped 时进行 segue(从 parse.com 获取数据)
【发布时间】:2014-11-27 17:05:27
【问题描述】:

单击并将数据发送到“Prepare to segue”时,Annotation 出现问题

我用过这段代码:

- (void)viewDidLoad {
[super viewDidLoad];

PFQuery *query = [PFQuery queryWithClassName:@"Ristoranti"];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

            if (!error)
            {

                NSLog(@"QUERY -----> :%@", objects);


                for(NSMutableDictionary *note1 in objects) {
                    float realLatitude1 = [[note1 objectForKey:@"Latitudine"] floatValue];
                    float realLongitude1 = [[note1 objectForKey:@"Longitudine"] floatValue];

                    NSLog(@"(PARSE) Latitudine: %f", realLatitude1);
                    NSLog(@"(PARSE) Longitudine: %f", realLongitude1);


                    DisplayMap *displayMap = [[DisplayMap alloc] init];

                    CLLocationCoordinate2D theCoordinate;
                    theCoordinate.latitude = realLatitude1;
                    theCoordinate.longitude = realLongitude1;
                    displayMap.coordinate = theCoordinate;
                    displayMap.title = [note1 objectForKey:@"NomeLocale"];
                    displayMap.subtitle = [note1 objectForKey:@"Indirizzo"];
                    displayMap.icon = [note1 objectForKey:@"PinMappa"];
                    [_mapView setDelegate:self];
                    [_mapView addAnnotation:displayMap];
                }
            }
        }];
}

和:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (![annotation isKindOfClass:[DisplayMap class]])
    {

        return nil;
    }

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

    MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

    if (pinView == nil)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        pinView.canShowCallout = YES;
    }
    else
        pinView.annotation = annotation;


    DisplayMap *myAnn = (DisplayMap *)annotation;
    pinView.image = [UIImage imageNamed:myAnn.icon];



    // Create a UIButton object to add on the
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [pinView setRightCalloutAccessoryView:rightButton];

    return pinView;
}

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

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
    {


        NSLog(@"The annotation tapped is: %@", view.annotation.title);


        NSUInteger index = [mapView.annotations indexOfObject:view.annotation];

        NSLog(@"%lu", (unsigned long)index);


        //[self performSegueWithIdentifier:@"Prova" sender:self];

    }  
}

和:

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

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

        DettagliAlberghi *destViewController = segue.destinationViewController;

        DisplayMap *displayMap = [[DisplayMap alloc] init];

        displayMap.title = [note1 objectForKey:@"NomeLocale"];
        displayMap.subtitle = [note1 objectForKey:@"Indirizzo"];
        displayMap.icon = [note1 objectForKey:@"PinMappa"];
        displayMap.Telefono = [note1 objectForKey:@"Telefono"];
        displayMap.Email = [note1 objectForKey:@"Email"];
        displayMap.Sito = [note1 objectForKey:@"Sito"];
        displayMap.TipologiaLocale = [note1 objectForKey:@"TipologiaLocale"];
        displayMap.Cucina = [note1 objectForKey:@"Cucina"];
        displayMap.Vegano = [note1 objectForKey:@"Vegano"];
        displayMap.Valutazione = [note1 objectForKey:@"Valutazione"];
        displayMap.Latitudine = [note1 objectForKey:@"Latitudine"];
        displayMap.Longitudine = [note1 objectForKey:@"Longitudine"];
        displayMap.Anteprima1 = [note1 objectForKey:@"Anteprima1"];
        displayMap.Anteprima2 = [note1 objectForKey:@"Anteprima2"];
        displayMap.Anteprima3 = [note1 objectForKey:@"Anteprima3"];
        displayMap.FaceB = [note1 objectForKey:@"Facebook"];
        displayMap.Twit = [note1 objectForKey:@"Twitter"];




        NSLog(@"Dettagli Ristorante: \n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@", displayMap.title, displayMap.subtitle, displayMap.Telefono, displayMap.Email, displayMap.Sito, displayMap.TipologiaLocale, displayMap.Cucina, displayMap.Vegano, displayMap.Valutazione, displayMap.Latitudine, displayMap.Longitudine, displayMap.Anteprima1, displayMap.Anteprima2, displayMap.Anteprima3, displayMap.FaceB, displayMap.Twit);




        destViewController.recipes = displayMap;

    }
}

我想了解如何识别某些注释被按下并通过“PrepareForSegue”将数据发送到下一页。 有谁知道如何帮助我?我要疯了。 提前致谢。

【问题讨论】:

    标签: xcode parse-platform mkmapview segue mkannotation


    【解决方案1】:

    您只需要使用 mapView:didSelectAnnotationView: 委托调用。

    https://developer.apple.com/documentation/mapkit/mkmapviewdelegate/1452393-mapview

    从 mapView:didSelectAnnotationView 中,通过 performSegueWithIdentifier 调用 segue 调用。

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

    假设您根据所选注释从 mapView:didSelectAnnotationView 中设置“note1”。

    【讨论】:

    • 无需使用:NSUInteger index = [mapView.annotations indexOfObject:view.annotation]; ?? PS:我正在从 Parse 获取数据......我不明白如何将它们发送到 PrepareForSegue
    • 不确定我是否理解。你需要它的索引吗?如果您只想要注释,那么: view.annotation 应该就是您所需要的......? (实际上,您已经让 prepareForSegue 引用了新控制器,并通过了“食谱”.. 所以我不确定您还需要什么?)
    • 我修改了代码:"[self performSegueWithIdentifier:@"Prova" sender:view.annotation.title];"在 PrepareForSegue 中我添加了:“MKAnnotationView * annotationView = sender;”。现在,当我单击 Pin 时,我得到本地的名称,但在所有要传递到下一个视图的食谱中,我得到 (null)。我不明白出了什么问题
    • 您正在 viewDidLoad 中实例化 DisplayMap/annotations,但在 prepareForSegue 中您正在使用 note1 对象再次实例化它。除非您使用 DisplayMap 子类或将 note1 分配到某个地方(在哪里?),否则这将为 null....
    • 是的,在 viewDidLoad 中,我读取了坐标,以便您指定要在地图上显示的图钉......但之后我需要在单击时读取并发送“ PrepareForSegue”,但我不明白出了什么问题……我把我用过的所有代码都写进去了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多