I am building a Tab Bar App and have a MapView set up on one of the tabs.
I've converted this over from a View-Based Application which worked perfectly so I know that it's mostly set up correctly.
Each pre-placed pin on the Map has annotations and a leftCalloutAccessoryView that links to the DetailViewController. What I want is for the callout screen (Detail View Controller) to just show up in that same tab but when I click on the left callout button the program crashes.
The method I'm using for touching the Callout Accessory is this:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
MillersLocations *annotationTapped = (MillersLocations *)view.annotation;
NSLog(@"button clicked on annotation %@", annotationTapped);
LocationsViewController *thisMap = (LocationsViewController *)[[UIApplication sharedApplication] delegate];
DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
dvc.title = view.annotation.title;
dvc.addressString = view.annotation.subtitle;
[thisMap switchViews:self.view toView:dvc.view]; //This is the line with the error message
}
On the line '[thisMap switchViews:self.view toView:dvc.view];' I get a SIGABRT error. And the errors:
2011-10-25 14:11:11.465 Miller Tab Bar[56230:207] -[Miller_Tab_BarAppDelegate switchViews:toView:]: unrecognized selector sent to instance 0x5835530
2011-10-25 14:11:11.467 Miller Tab Bar[56230:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Miller_Tab_BarAppDelegate switchViews:toView:]: unrecognized selector sent to instance 0x5835530'
Is the switchViews method invalid in a Tab Bar app? If so, what method is generally used in a Tab Bar app to switch views within a certain tab? Thanks for the help, I'm still learning!
I realized I'm calling it to 'self.view' so here's how I've updated the code. I had LocationsViewController set as the delegate because of copy and paste error.:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
MillersLocations *annotationTapped = (MillersLocations *)view.annotation;
NSLog(@"button clicked on annotation %@", annotationTapped);
LocationsViewController *thisMap = [[LocationsViewController alloc]initWithNibName:@"LocationsViewController" bundle:nil];
DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
dvc.title = view.annotation.title;
dvc.addressString = view.annotation.subtitle;
[thisMap switchViews:self.view toView:dvc.view];
}
Now I don't get the error but it doesn't switch views. It logs out and everything but doesn't go anywhere. The method is declared in LocationsViewController.h and implemented in LocationsViewController.m. Why wouldn't it be linking to anything?
If the callout method is in the same class as switchViews
then you don't need to / shouldn't create a new instance of LocationsViewController
inside itself. Instead of [thisMap switchViews...
, it should just be [self switchViews...
.
However, without knowing exactly what that method does, I can't be sure it will do what's expected (eg. what will it do with the self.view
passed to it--will it be able to switch back).
Just a couple of other alternatives (among many) whether you're using a tab bar or not are:
Show the detail view as a modal view controller. This is easily done by:
DetailViewController *dvc = [[DetailViewController alloc] init...
dvc.title = view.annotation.title;
dvc.addressString = view.annotation.subtitle;
[self presentModalViewController:dvc animated:YES];
[dvc release];
The modal view would be dismissed using [self dismissModalViewControllerAnimated:YES];
.
Put a UINavigationController
in the maps tab and make LocationsViewController
the root view controller of that navigation controller and push the detail view controller. The detail view would then be popped when done. Implementing this requires a bit more work but will allow the user to use the other tabs even while the detail view is showing on the map tab.
I suggest going through Apple's View Controller Programming Guide as well as their introductions to the language: Learning Objective-C: A Primer, and The Objective-C Programming Language.