Search code examples
iosannotationscompiler-errorspushviewcontroller

'NSInvalidArgumentException'


I have some annotationView in the map and I want that with touchUpInside open a new ViewController but I get this error:

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
 [MapViewController loadDetailListViewController:]: unrecognized selector sent 
 to instance 0xa042380'

This is the code in MapViewController.m:

 -(void)loadDetailListViewController{


      if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

    DetailListViewController *detailList =[[DetailListViewController 
    alloc]initWithNibName:@"DetailListViewController~iPhone" bundle:nil];
    detailList.title = self.chinaTable.title;
    detailList.chinaTable = self.chinaTable;


    [self.navigationController pushViewController:detailList animated:YES];

}else {

    DetailListViewController *detailList =[[DetailListViewController 
    alloc]initWithNibName:@"DetailListViewController~iPad" bundle:nil];
    detailList.title = self.chinaTable.title;
    detailList.chinaTable = self.chinaTable;

    [self.navigationController pushViewController:detailList animated:YES];
}

}

 - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
    <MKAnnotation>)annotation {

       //......

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
       [rightButton addTarget:self action:@selector(loadDetailListViewController:) 
      forControlEvents:UIControlEventTouchUpInside]; //the error is here
      //....
     }

Solution

  • Change

    @selector(loadDetailListViewController:) 
    

    to

    @selector(loadDetailListViewController)
    

    Reason: @selector(abc) gives the selector of a method abc without any parameters. @selector(abc:) gives the selector of the method abc with one parameter. Consequentially @selector(abc::) gives the selector of the method abc with two parameter objects.

    Objective-C is polymorph. Meaning the same method may exist multiple times. That means they have the same name and are implemented multiple times to provide variations of the method depending on the number of parameters (or on the names of parameters if the names of parameters are given in the selector statement too).

    Strictly spoken abc and abc: and abc:: may be totally different and independent from each other. But that would be very bad style. It is rather common that the methods do more or less the same and their functionality is just varied in details driven by the different values that are passed to it.