【问题标题】:How can I perform a segue based on a MKAnnotation pin?如何基于 MKAnnotation 引脚执行 segue?
【发布时间】:2018-11-04 21:51:32
【问题描述】:

我有一个包含 2 个事件的地图,它们的引脚彼此非常接近。

此代码将检索两个引脚的位置:

// This query is used to get the location of all events in the parse cloud database
PFQuery *locationQuery = [PFQuery queryWithClassName:@"Event"];
[locationQuery whereKeyExists:@"EventLocation"];
[locationQuery whereKey:@"Organizator" notEqualTo:PFUser.currentUser.username];
[locationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // All the locations are stored in the objects NSArray

        for (PFObject *gp in objects) {

            // We store all the found locations in a PFGeoPoint object and then we have to transform this PFGeoPoints in real latitude/longitude coordinates via the CLLocationCoordinate2DMake method
            self.location = [gp objectForKey:@"EventLocation"];
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.location.latitude, self.location.longitude);

            // Subclass of MKAnnotation
            AnnotationMap *AllEventsAround = [[AnnotationMap alloc]init];

            AllEventsAround.coordinate = coordinate; // We assign the coordinates got in CLLocationCoordinate2DMake to the coordinates of the AnnotationMap class
            AllEventsAround.title = [gp valueForKey:@"SportName"];
            AllEventsAround.subtitle = [NSString stringWithFormat:@"%@",[gp valueForKey:@"DateAndTime"]];

            // method call to point the annotations on the map
            [self.EventsmapMapView addAnnotation:AllEventsAround];
        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

检索工作正常。一个例子可以是:

=> PFGeoPoint:0x2805XXXX,纬度:50.XXXXX,经度:19.XXXXX

=> PFGeoPoint:0x280XXXX,纬度:50.061XXXX,经度:19.93XXXX

当我点击一个 pin 时,它会根据此代码执行 segue:

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

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

    EventDetailsTableViewController *controller = (EventDetailsTableViewController *)segue.destinationViewController;
    controller.Pointfrommap = self.location;
  }
} 

我的问题:

self.location来自上述查询(PFQuery)的结果,因此它会保存从查询中获得的最后一个值,而不是来自一个pin tap的值

我的猜测:

我知道我可以使用这个 Delegate 方法:

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

    //In case a pin is selected, we perform a segue to the event's detail view. On the segue, the actual event coordinates are stored in tappedCoord and send to the details view controller

    [self performSegueWithIdentifier:@"toEventDetails" sender:self];   // Use your appropriate segue identifier
}

也许我可以在此处添加一些内容以传递正确的位置。

【问题讨论】:

    标签: ios objective-c mkmapview segue mkannotation


    【解决方案1】:

    你可以试试

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    
         AnnotationMap *allEventsAround = (AnnotationMap*)view.annotation;
         PFGeoPoint *loc  = [PFGeoPoint geoPointWithLatitude:allEventsAround.coordinate.latitude longitude:allEventsAround.coordinate.longitude];
        [self performSegueWithIdentifier:@"toEventDetails" sender:loc];   // Use your appropriate segue identifier
    }
    

    然后在prepareForSegue里面

    controller.Pointfrommap =  (PFGeoPoint*) sender;
    

    【讨论】:

    • 由于某种原因,我在didSelectAnnotationView 中得到了Sending 'CLLocationCoordinate2D' (aka 'struct CLLocationCoordinate2D') to parameter of incompatible type 'id _Nullable':/
    • 查看编辑 ....................这里我想 Pointfrommap 的类型是 CLLocation
    • 哦,对了!好吧,Pointfrommap 恐怕是 PFGeoPoint 类型。但我会尝试以同样的方式解决它
    • 你明白了。谢谢:)
    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2012-08-31
    相关资源
    最近更新 更多