在“prepareForSegue"中如何访问当前注释的 NSString? [英] How do I access the current annotation's NSString when in "prepareForSegue"?
问题描述
引脚 .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 图
How I set my Pin in the view
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 将保存特定引脚的时间.如何将当前 pin 的时间 NSString 传递给下一个视图的 NSString?
Now in the second view controller that is accessed with the identifier "Details", I have a NSString that will hold the time of the specific pin. How can I pass the current pin's time NSString to the next view's NSString?
注意:在地图上的不同时间只会设置多个图钉,我尝试过 使用storyboard将数据从注释传递到详细视图iOS并尝试使用
Note: There will but multiple pins set at different times on the map and I have tried Passing data from annotations to detail view iOS using storyboard and tried to use
Pin *an = (Pin *)mapView.annotation;
但它只允许我使用一个数组作为 mapView 注释
but it only lets me used an array for the mapView annotation
Pin *an = (Pin *)mapView.annotations;
请帮忙!
推荐答案
您可以通过注解视图的 annotation
属性访问您的注解,然后将其作为发送者传递给 performSegueWithIdentifier
像这样
You can access your annotation via the annotation
property of the annotation view and then pass this as the sender to performSegueWithIdentifier
like so
- (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;
}
}
这篇关于在“prepareForSegue"中如何访问当前注释的 NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!