Home Forums iOS Development Add a detail view in Map Annotations

This topic contains 8 replies, has 2 voices, and was last updated by  Chris 1 year, 2 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2009 Reply

    Chris

    Hello,

    I have created a simple app that uses MapKit and has some fixed annotations placed by the developer. My issue is that I want to click the Detail disclosure Button and get a different view in which there will be a picture, a title and some text. Sort of a “detailed view” for the annotation.
    This is my App so far

    and also there is a class called Shops that handles the annotations on the map, I didn’t include it here.

    Thanks in advance!

    #2011 Reply

    Malek
    Keymaster

    Hi 🙂

    Each time a pin annotation is about to appear on the map, the protocol method mapView(_:viewForAnnotation:) is called. In this method, you can customise the returned view with yours. For example:

    The code above will return a custom annotation view, something like the following:

    Custom MKAnnotationView

    The MKAnnotationView is a UIView subsclass, which means that you can add to it any type of subviews you want (buttons, image views, labels, etc).

    Hope this help, let me know if you have more questions. I will be glad to help 🙂

    • This reply was modified 1 year, 2 months ago by  Malek.
    #2013 Reply

    Chris

    Great thanks a lot!, I will utilise that right away

    If someone wanted to dive deeper into that, for example having a table viewer like this one :

    Setup a storyboard segue

    And you want that to show up when u press the .DetailDisclosure Button, how can you do that??
    I tried using “prepareforsegue” but to no avail.

    For this table I have used “present modally segue”

    Thanks in advance

    • This reply was modified 1 year, 2 months ago by  Malek. Reason: Fixed the attached picture broken link. Include the op picture in sweettutos media library and include it on the post
    #2016 Reply

    Malek
    Keymaster

    Absolutely, you can trigger a segue on the disclosure button click. All you have to do is define your segue (with an identifier) in storyboard as you did in the screenshot, then from the code, you need to call the segue from the mapView(_:annotationView:calloutAccessoryControlTapped:) protocol method, like below (Assuming the segue identifier is ShowDetails):

    The prepareForSegue method is only called when a segue in about to be performed, so you cannot rely on this method to trigger the segue. However, it’s recommended that you override that method (I mean the prepareForSegue) to make any setup you want before you move to the table view controller. For example:

    Hope this help 🙂

    Note: If you are interested in more details, I have detailed all of this in my tutorial here.

    • This reply was modified 1 year, 2 months ago by  Malek. Reason: fix formatting
    #2023 Reply

    Chris

    Ok I tried it but it didn’t seem to work. (I am really sorry if I waste your time 🙁 ) Here below I injected your code as well as changed the identifier name, still nothing happens to the button. I still cannot move to Prototype Cell. Ill also post the class for the Prototype Cell in next comment

    here is the full code which i enriched it a bit, (ill let you know which parts)

    #2024 Reply

    Chris

    This is the class, I have not issue with it, but just posting it so you can see the full work, i have done

    #2041 Reply

    Malek
    Keymaster

    Hi Chris,

    Can you set a UIButton as the rightCalloutAccessoryView or as the leftCalloutAccessoryView and try again ? let me know how it goes. If the issue persists, please attach a working sample project and I will be glad to assist you further 🙂

    #2094 Reply

    Malek
    Keymaster

    Hi Chris, the method you are using to track the click on the callout accessory seems to be wrong. The right method is the mapView(_:annotationView:calloutAccessoryControlTapped:). Also, you have some mistakes regarding the closing brackets of the class and methods (some methods are nested into another which shouldn’t be). I fixed the project for you, please download it here.

    #2115 Reply

    Chris

    Thanks a lot Malek, you are incredible! Have a nice weekend!

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Add a detail view in Map Annotations
Your information: