在ViewController之间传递地图数据

时间:2013-04-22 12:35:36

标签: objective-c ios6

我几乎跟着这个post。 viewcontroller将通过故事板推送。这里的相关代码:
ViewController.m:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"%@", view.annotation.title);
    MapPoint *annView = view.annotation;
    DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    dvc.title = @"my own Details";
    dvc.titleTMP = annView.title;
    dvc.subtitleTMP = annView.subtitle;

    [self.navigationController pushViewController:dvc animated:YES];
}

MapPoint.h:

@interface MapPoint : NSObject <MKAnnotation>
{
    NSString *_title;
    NSString *_subtitle;
    CLLocationCoordinate2D _coordinate;
    UIImage *_storeImage;
}

@property (copy) NSString *title;
@property (copy) NSString *subtitle;
@property (nonatomic, readonly) UIImage *storeImage;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;


- (id)initWithTitle:(NSString*)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate storeImage:(UIImage*)storeImage;

和DetailViewController:

@interface DetailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *branchImage;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UITextView *textView;

我认为我的错误在 calloutAccessoryControlTapped 方法中,但我不知道这个问题的真正原因是什么。

2 个答案:

答案 0 :(得分:1)

如果您使用故事板,可以使用prepareForSegue方法并在类中设置,就像您所做的那样。

我发布了一部分代码

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

if([segue.identifier isEqualToString:@"Brs"]){
    NSLog(@"segue %@ ",[segue identifier]);
    [segue.destinationViewController setPath:[ris_xml objectAtIndex:index_post-1] ];
    }
}

在这个例子中,我只设置下一个UIViewController的属性“Path”,如果他的标识符是“Brs”。 使用此方法需要将UIviewController标识符设置到storyboard右侧面板中。 如果你在故事板中使用它,则不需要实例化新的UIViewController。

答案 1 :(得分:0)

我通过考虑加载序列来解决它。
所有要做的就是创建临时的@properties

@interface DetailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *branchImage;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic) NSString *titleTMP;        //added
@property (nonatomic) NSString *subtitleTMP;     //added

并做

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"%@", view.annotation.title);
    MapPoint *annView = view.annotation;
    DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    dvc.title = @"my own Details";
    dvc.titleTMP = annView.title;
    dvc.subtitleTMP = annView.subtitle;

    [self.navigationController pushViewController:dvc animated:YES];
}

DetailViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    titleLabel.text = titleTMP;
    textView.text = subtitleTMP;

}