繁体   English   中英

在“ prepareForSegue”中时,如何访问当前注释的NSString?

[英]How do I access the current annotation's NSString when in “prepareForSegue”?

Pin .h

#import <Foundation/Foundation.h>
@import MapKit;

@interface Pin : NSObject <MKAnnotation> {

NSString *time;
CLLocationCoordinate2D coordinate;
}

@property (nonatomic, copy)   NSString *time;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@end

Pin.m

#import "Pin.h"

@implementation Pin

@synthesize coordinate;
@synthesize time;
@end

我如何在视图中设置Pin

Pin *newPin = [[Pin alloc]init];
            newPin.coordinate = userCoordinate;

    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-DD-yyy hh:mm a"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];
    newPin.time = resultString;
    [self.mapView addAnnotation:newPin];

当我按标注时会发生什么

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
      if ([segue.identifier isEqualToString:@"Details"])
     {
        InfoViewController *vc = segue.destinationViewController;
     }
}

现在,在使用标识符“ Details”访问的第二个视图控制器中,我有一个NSString,它将保存特定引脚的时间。 如何将当前引脚的时间NSString传递给下一个视图的NSString?

注意:地图上的不同时间只会设置多个图钉,并且我尝试使用情节提要将数据从注释传递到iOS的详细视图iOS并尝试使用

Pin *an = (Pin *)mapView.annotation;

但是它只允许我为mapView注释使用数组

Pin *an = (Pin *)mapView.annotations;

请帮忙!

您可以通过注释视图的annotation属性访问注释,然后将其作为发件人传递给performSegueWithIdentifier如下所示

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view.annotation];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
      if ([segue.identifier isEqualToString:@"Details"])
     {
        Pin *myPin=(Pin *)sender;
        InfoViewController *vc = segue.destinationViewController;
        vc.time=myPin.time;
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM