如何在“prepareForSegue”中访问当前注释的NSString?
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使用故事板,并尝试使用
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; } }