I am trying to pass an object between two ViewControllers in a Storyboard in objective-C iPhone app. (iOS 5/ XCode 4)
The first ViewController is a map with annotations (each annotation object is called MyLocation, it is displayed on a map by a containing MyLocationView). The user clicks an annotation, a callout appears, then user clicks the right arrow to load an annotation detail view (a new ViewController), which displays more details on the selected annotation.
I have defined a segue ‘SegueAnnotationDetail’ which moves between the main View Controller and the annotation details view controller.
I have defined an instance variable in the main view controller to hold the annotation that the user clicked, and then I am going to pass it to the annotation details view (ViewController.h):
@interface ViewController : UIViewController <MKMapViewDelegate> {
MKAnnotationView *selectedAnnotation;
}
So in the main view controller (ViewController.m) I have the following:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
selectedAnnotation = view;
[self performSegueWithIdentifier:@"SegueAnnotationDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"SegueAnnotationDetail"])
{
LocationDetailViewController *vc = [segue destinationViewController];
[vc setLocation:selectedAnnotation];
}
}
Then in LocationDetailViewController (which is the 2nd view controller displayed the details for each annotation), I have
@interface LocationDetailViewController : UIViewController
@property MKAnnotationView* location;
@end
Each annotation is a MKPinAnnotationView and is created as follows, in the viewForAnnotation method, also inside ViewController.m
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
// removed some code related to animation view queue here for simplicity
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
The problem is, when this runs, the app just crashes at the line:
[vc setLocation:selectedAnnotation];
UPDATE
The error message in the console is:
2012-08-26 18:33:10.615 ArrestsPlotter[79918:c07] -[UIViewController setLocation:]: unrecognized selector sent to instance 0x6ea0b60
2012-08-26 18:33:10.615 ArrestsPlotter[79918:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setLocation:]: unrecognized selector sent to instance 0x6ea0b60'
*** First throw call stack:
(0x165e022 0x12cecd6 0x165fcbd 0x15c4ed0 0x15c4cb2 0x3977 0x85c4be 0x4f95ab 0x385b 0x361f8c 0x36d8ba 0x165fe99 0x43414e 0x4340e6 0x4daade 0x4dafa7 0x4da266 0x4593c0 0x4595e6 0x43fdc4 0x433634 0x1da5ef5 0x1632195 0x1596ff2 0x15958da 0x1594d84 0x1594c9b 0x1da47d8 0x1da488a 0x431626 0x231d 0x2285)
terminate called throwing an exception(lldb)
When I comment out the offending line, the app runs ok albeit without showing the right info on the details page.
WTF am I doing wrong?
Ok, I answered it myself. I’ll post the answer here since it’s not that clear on the internet.
To associate a view controller file with a specified scene in the storyboard, click on the scene, then use the Attribute Inspector, select Custom Class and choose the class to use.
That is how you link a view controller with a scene. That was what was causing the problem here (see comments to the first answer)