Unrecognized selector sent to instance while trying to open apple maps app in my app

Harshil.Chokshi picture Harshil.Chokshi · Apr 15, 2016 · Viewed 8.3k times · Source

I am trying to open up apple maps within the map that I created, and in the process of doing so I am getting an error saying that "unrecognized selector send to instance". I am using arc so there should be no memory management problem. Here is the code I wrote in viewDidLoad. I created a button and added a selector to it..

UIButton *appleMapsButton = [[UIButton alloc] init];
appleMapsButton.frame = CGRectMake(100, 100, 200, 200);
appleMapsButton.backgroundColor = [UIColor redColor];
[appleMapsButton addTarget:self action:@selector(appleMapsClicked:) forControlEvents:UIControlEventTouchUpInside];
[mapView addSubview:appleMapsButton];

Here is the action method:

-(void)appleMapsClicked{

CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(lat, lng);
MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil];
MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation];

NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey];

[endingItem openInMapsWithLaunchOptions:launchOptions];
}

Here is the error I am getting in the debugger area:

-[MapViewController appleMapsClicked:]: unrecognized selector sent to instance 0x176564d0 2016-04-15 16:10:39.394 Restaurant[1081:220822] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MapViewController appleMapsClicked:]: unrecognized selector sent to instance 0x176564d0'

Answer

mbrennek picture mbrennek · Apr 15, 2016

You are registering to receive the message with the method appleMapsClicked:, but your method does not have a parameter.

Removing the :, or changing the method signature to -(void)appleMapsClicked:(id)sender should fix it.