Harshil.Chokshi Harshil.Chokshi - 4 months ago 48
iOS Question

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

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:


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'


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.