Search code examples
objective-cios6

Passing map-data between ViewController


I nearly followed this post. The viewcontroller will push via storyboard. Here the relevant code:
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;

and DetailViewController:

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

I think my mistake is in the calloutAccessoryControlTapped method but I dont know what is the real reason for this issue.


Solution

  • I solved it by thinking about loading sequence.
    All what to do is create temporary @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
    

    and do

    -(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];
    }
    

    The DetailViewController:

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