【问题标题】:How do I access the current annotation's NSString when in "prepareForSegue"?在“prepareForSegue”中如何访问当前注释的 NSString?
【发布时间】:2014-08-18 18:53:03
【问题描述】:

引脚.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;
     }
}

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

注意:地图上会在不同时间设置多个图钉,我尝试过Passing data from annotations to detail view iOS using storyboard并尝试使用

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

但它只允许我使用一个数组作为 mapView 注释

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

请帮忙!

【问题讨论】:

    标签: ios objective-c annotations segue


    【解决方案1】:

    您可以通过注释视图的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;
         }
    }
    

    【讨论】:

    • 我认为您在代码的最后一行是指“vc.time = myPin.time”。程序崩溃,我得到“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[MKPinAnnotationView time]:无法识别的选择器发送到实例 0x15d25420'”
    • 谢谢。解决了这个问题。您是否将发件人更改为 view.annotation ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多