Reputation: 13354
I have looked at all the other posts related to passing data between viewcontrollers but none of them are working for my problem.
I want to pass the title of the annotation someone taps in mapKit to a new viewcontroller so I can use that title in the new viewcontroller. I am using the calloutAccessoryControlTapped
method to detect when someone has tapped the detailDisclosure button and then using the following code to add the title to the property in the second viewcontroller (called detailViewController):
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
detailViewController *viewController = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];
detailViewController *detailView = [[detailViewController alloc] init];
// THIS IS WHERE I SET THE ANNOTATION TITLE TO THE PROPERTY IN THE SECOND VIEWCONTROLLER
detailView.titleTime = view.annotation.title;
NSLog(@"view.annotation.title: %@", view.annotation.title); // SHOWS THE TITLE
NSLog(@"detailView.titleTime: %@", detailView.titleTime); // SHOWS THE TITLE
// HERE I PRESENT NEW VIEWCONTROLLER (HOPING THE TITLE IS PASSED)
[self presentViewController:viewController animated:YES completion:nil];
}
In the second viewcontroller (detailViewController) I have this in the viewDidLoad
method:
- (void)viewDidLoad
{
[super viewDidLoad];
// set the Title Label to the titleTime (passed from the mainViewController)
self.titleForDetail.text = self.titleTime;
NSLog(@"titleForDetail.text %@:", titleForDetail.text); // THIS IS NULL
NSLog(@"titleForDetail.text %@:", titleTime); // THIS IS NULL
}
Why are self.titleForDetail.text
and self.titleTime NULL
? Why is nothing being passed from the original viewController?
Upvotes: 0
Views: 118
Reputation: 104092
You have these two lines in your code:
detailViewController *viewController = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];
detailViewController *detailView = [[detailViewController alloc] init];
And in the rest of the code, you sometimes use viewController, and sometimes detailView. You should only have one of those, and it should be instantiated like you do for viewController:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
detailViewController *detailView = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];
detailView.titleTime = view.annotation.title;
[self presentViewController:detailView animated:YES completion:nil];
}
Upvotes: 1
Reputation:
In calloutAccessoryControlTapped
, the code is setting the titleTime
property on detailView
but then presenting viewController
(which does not have the property set).
Change the present to:
[self presentViewController:detailView animated:YES completion:nil];
It looks like you don't need the storyboard
and viewController
variables so you can remove them from that method.
Upvotes: 1