开发者

Passing String Value From Map View to Detail View

I am attempting to pass a string variable from a Map View Controller to a Detail View Controller after the click of a callout button. I took the title of the callout button and stored it in an NSString object (which had been synthesized in the Map View Controller).

I then allocated and initialized the Map View Controller in the Detail View Controller and attempted to log the value once in the new view controller and it returned as null. Here's the code:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView 开发者_JAVA技巧*)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    TheLocations *annotationTapped = (TheLocations *)view.annotation;

    NSLog(@"button clicked on annotaion %@", annotationTapped);

    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    dvc.title = view.annotation.title;
    pinTitle = dvc.title;
    NSLog(@"%@", pinTitle);

    [thisMap switchViews:self.view toView:dvc.view];
}

This results in a successful log of "pinTitle"'s value. pinTitle has been created and synthesized, so it's a string of this Map View.

Now, I switch over to the Detail View and entered this code into the viewDidLoad method:

MapViewController *mvc = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
    NSLog(@"%@", mvc.pinTitle);

It recognizes "pinTitle" and allows it to log out but returns "(null)" so somewhere along the way the value of pinTitle was lost. How do I get pinTitle to retain it's value when I switch views? Thanks so much for the help!


In the detail view's viewDidLoad method, you are creating a new instance of MapViewController which is separate from the instance being referenced in calloutAccessoryControlTapped.

In that new, separate instance, pinTitle is not set and has a default value of null.

I'm not sure why you need the pinTitle variable in any case.

In calloutAccessoryControlTapped, you are setting dvc.title so in the viewDidLoad of the DetailViewController, you would access that value using self.title. You don't need to reference MapViewController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜