Pass annotation title to embedded containerview when selected Swift












0















I have tried using the func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) to pass the annotation title to the embedded container view. However when i build and run, it doesn't work.



What am I doing incorrectly? Is this the right approach?



I've tried func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) see code



var annotationTitle = "Default"



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
if let annotation = view.annotation {
annotationTitle = annotation.title!!
}

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapContainer" {
let destination = segue.destination as! MapDetailContainerViewController
destination.selectedAnnotation = annotationTitle as String
}
}


}



The data is being passed to the containerviewcontroller as "Deafult" instead of annotation.title value










share|improve this question























  • Thanks for the insight. The container and map are shown at the same time. I want to be able to click the various map pins and have the container show information about the pin currently selected.

    – Freddie726
    Jan 2 at 17:25


















0















I have tried using the func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) to pass the annotation title to the embedded container view. However when i build and run, it doesn't work.



What am I doing incorrectly? Is this the right approach?



I've tried func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) see code



var annotationTitle = "Default"



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
if let annotation = view.annotation {
annotationTitle = annotation.title!!
}

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapContainer" {
let destination = segue.destination as! MapDetailContainerViewController
destination.selectedAnnotation = annotationTitle as String
}
}


}



The data is being passed to the containerviewcontroller as "Deafult" instead of annotation.title value










share|improve this question























  • Thanks for the insight. The container and map are shown at the same time. I want to be able to click the various map pins and have the container show information about the pin currently selected.

    – Freddie726
    Jan 2 at 17:25
















0












0








0








I have tried using the func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) to pass the annotation title to the embedded container view. However when i build and run, it doesn't work.



What am I doing incorrectly? Is this the right approach?



I've tried func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) see code



var annotationTitle = "Default"



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
if let annotation = view.annotation {
annotationTitle = annotation.title!!
}

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapContainer" {
let destination = segue.destination as! MapDetailContainerViewController
destination.selectedAnnotation = annotationTitle as String
}
}


}



The data is being passed to the containerviewcontroller as "Deafult" instead of annotation.title value










share|improve this question














I have tried using the func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) to pass the annotation title to the embedded container view. However when i build and run, it doesn't work.



What am I doing incorrectly? Is this the right approach?



I've tried func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) see code



var annotationTitle = "Default"



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
if let annotation = view.annotation {
annotationTitle = annotation.title!!
}

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapContainer" {
let destination = segue.destination as! MapDetailContainerViewController
destination.selectedAnnotation = annotationTitle as String
}
}


}



The data is being passed to the containerviewcontroller as "Deafult" instead of annotation.title value







swift xcode mkannotationview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 16:48









Freddie726Freddie726

82




82













  • Thanks for the insight. The container and map are shown at the same time. I want to be able to click the various map pins and have the container show information about the pin currently selected.

    – Freddie726
    Jan 2 at 17:25





















  • Thanks for the insight. The container and map are shown at the same time. I want to be able to click the various map pins and have the container show information about the pin currently selected.

    – Freddie726
    Jan 2 at 17:25



















Thanks for the insight. The container and map are shown at the same time. I want to be able to click the various map pins and have the container show information about the pin currently selected.

– Freddie726
Jan 2 at 17:25







Thanks for the insight. The container and map are shown at the same time. I want to be able to click the various map pins and have the container show information about the pin currently selected.

– Freddie726
Jan 2 at 17:25














1 Answer
1






active

oldest

votes


















0














You said:




The container and map are shown at the same time.




If they’re both created at the same time, then prepare(for:sender:) is undoubtedly getting called before didSelect is. You can confirm that with some breakpoints or judicious print statements.



So you can have prepare(for:sender:) save a reference to segue.destination as? MapDetailContainerViewController in some local variable and then didSelect can set selectedAnnotation



var embeddedViewController: MapDetailContainerViewController?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapContainer" {
embeddedViewController = segue.destination as? MapDetailContainerViewController
}
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation,
let title = annotation.title {
embeddedViewController?.selectedAnnotation = title
}
}


Or you can bypass the prepare(for:sender) and just use children (previously called childViewControllers):



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
if let embeddedViewController = children.first as? MapDetailContainerViewController,
let annotation = view.annotation,
let title = annotation.title {
embeddedViewController.selectedAnnotation = title
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010123%2fpass-annotation-title-to-embedded-containerview-when-selected-swift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You said:




    The container and map are shown at the same time.




    If they’re both created at the same time, then prepare(for:sender:) is undoubtedly getting called before didSelect is. You can confirm that with some breakpoints or judicious print statements.



    So you can have prepare(for:sender:) save a reference to segue.destination as? MapDetailContainerViewController in some local variable and then didSelect can set selectedAnnotation



    var embeddedViewController: MapDetailContainerViewController?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showMapContainer" {
    embeddedViewController = segue.destination as? MapDetailContainerViewController
    }
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annotation = view.annotation,
    let title = annotation.title {
    embeddedViewController?.selectedAnnotation = title
    }
    }


    Or you can bypass the prepare(for:sender) and just use children (previously called childViewControllers):



    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
    if let embeddedViewController = children.first as? MapDetailContainerViewController,
    let annotation = view.annotation,
    let title = annotation.title {
    embeddedViewController.selectedAnnotation = title
    }
    }





    share|improve this answer






























      0














      You said:




      The container and map are shown at the same time.




      If they’re both created at the same time, then prepare(for:sender:) is undoubtedly getting called before didSelect is. You can confirm that with some breakpoints or judicious print statements.



      So you can have prepare(for:sender:) save a reference to segue.destination as? MapDetailContainerViewController in some local variable and then didSelect can set selectedAnnotation



      var embeddedViewController: MapDetailContainerViewController?

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "showMapContainer" {
      embeddedViewController = segue.destination as? MapDetailContainerViewController
      }
      }

      func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
      if let annotation = view.annotation,
      let title = annotation.title {
      embeddedViewController?.selectedAnnotation = title
      }
      }


      Or you can bypass the prepare(for:sender) and just use children (previously called childViewControllers):



      func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
      if let embeddedViewController = children.first as? MapDetailContainerViewController,
      let annotation = view.annotation,
      let title = annotation.title {
      embeddedViewController.selectedAnnotation = title
      }
      }





      share|improve this answer




























        0












        0








        0







        You said:




        The container and map are shown at the same time.




        If they’re both created at the same time, then prepare(for:sender:) is undoubtedly getting called before didSelect is. You can confirm that with some breakpoints or judicious print statements.



        So you can have prepare(for:sender:) save a reference to segue.destination as? MapDetailContainerViewController in some local variable and then didSelect can set selectedAnnotation



        var embeddedViewController: MapDetailContainerViewController?

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showMapContainer" {
        embeddedViewController = segue.destination as? MapDetailContainerViewController
        }
        }

        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if let annotation = view.annotation,
        let title = annotation.title {
        embeddedViewController?.selectedAnnotation = title
        }
        }


        Or you can bypass the prepare(for:sender) and just use children (previously called childViewControllers):



        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
        if let embeddedViewController = children.first as? MapDetailContainerViewController,
        let annotation = view.annotation,
        let title = annotation.title {
        embeddedViewController.selectedAnnotation = title
        }
        }





        share|improve this answer















        You said:




        The container and map are shown at the same time.




        If they’re both created at the same time, then prepare(for:sender:) is undoubtedly getting called before didSelect is. You can confirm that with some breakpoints or judicious print statements.



        So you can have prepare(for:sender:) save a reference to segue.destination as? MapDetailContainerViewController in some local variable and then didSelect can set selectedAnnotation



        var embeddedViewController: MapDetailContainerViewController?

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showMapContainer" {
        embeddedViewController = segue.destination as? MapDetailContainerViewController
        }
        }

        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if let annotation = view.annotation,
        let title = annotation.title {
        embeddedViewController?.selectedAnnotation = title
        }
        }


        Or you can bypass the prepare(for:sender) and just use children (previously called childViewControllers):



        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
        if let embeddedViewController = children.first as? MapDetailContainerViewController,
        let annotation = view.annotation,
        let title = annotation.title {
        embeddedViewController.selectedAnnotation = title
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 18:05

























        answered Jan 2 at 17:56









        RobRob

        305k51576745




        305k51576745
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010123%2fpass-annotation-title-to-embedded-containerview-when-selected-swift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith