1
votes

I have a mapView with a detail disclosure indicator that when touched needs to bring the MoreDetailViewController on the stack. At the moment it crashes with a unrecognized selector sent to instance error.

I'm trying to figure out how to call this method - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender from the disclosure indicator press.

Here's the Map annotation code with the disclosure indicator:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *mapPin = nil;
    if(annotation != map.userLocation) 
    {
        static NSString *defaultPinID = @"defaultPin";
        mapPin = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if (mapPin == nil )
        {
            mapPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                      reuseIdentifier:defaultPinID];
            mapPin.canShowCallout = YES;

            UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [disclosureButton addTarget:self action:@selector(prepareForSegue:) forControlEvents:UIControlEventTouchUpInside];

            mapPin.rightCalloutAccessoryView = disclosureButton;

        }
        else
            mapPin.annotation = annotation;

    }
    return mapPin;
}

Here's the method that should be called:

// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure we're referring to the correct segue
    if ([[segue identifier] isEqualToString:@"ShowMoreInfo"]) {

        // Get reference to the destination view controller
        MoreDetailViewController *mdvc = [segue destinationViewController];


        [mdvc setSelectedItemName:[NSString stringWithFormat:@"%@", placeName.text]];
        [mdvc setSelectedItemAddress:[NSString stringWithFormat:@"%@", placeFormattedAddress.text]];

        [mdvc setSelectedItemWeb:[NSString stringWithFormat:@"%@", placeWebsite.text]];
        [mdvc setSelectedItemRating:[NSString stringWithFormat:@"%@", placeRating.text]];
  //      [mdvc setSelectedItemDistance:[NSString stringWithFormat:@"%@", placeDistance.text]];

    }
}
2
Here's the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailViewController prepareForSegue:]: unrecognized selector sent to instance 0x81bbb30'hanumanDev

2 Answers

0
votes

You have two kind of detail view controller there, a MoreDetailViewController in your code, and a DetailViewController in your error. Is it possible that you're mixing them up?

0
votes

prepareForSegue: and prepareForSegue:sender: are not the same methods. Also placeName and friends in your prepareForSegue:sender: method are unknown (or are ivars).

I would radically change the prepareForSegue:sender: method and change it to

- (void)showMoreInfo:(id)sender{
        // Get reference to the destination view controller
        MoreDetailViewController *mdvc = [segue destinationViewController];

//define and set the placename and other variables

        [mdvc setSelectedItemName:[NSString stringWithFormat:@"%@", placeName.text]];
        [mdvc setSelectedItemAddress:[NSString stringWithFormat:@"%@", placeFormattedAddress.text]];

        [mdvc setSelectedItemWeb:[NSString stringWithFormat:@"%@", placeWebsite.text]];
        [mdvc setSelectedItemRating:[NSString stringWithFormat:@"%@", placeRating.text]];
  //      [mdvc setSelectedItemDistance:[NSString stringWithFormat:@"%@", placeDistance.text]];

    }
}

And call it the same way, just changing the name of the selector called to

[disclosureButton addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];

And if you're into annotations and maps, you shouldn't use segues which are more for simple navigation.