Pass annotation title to embedded containerview when selected Swift
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
add a comment |
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
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
add a comment |
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
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
swift xcode mkannotationview
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
}
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
}
add a comment |
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
}
}
add a comment |
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
}
}
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
}
}
edited Jan 2 at 18:05
answered Jan 2 at 17:56


RobRob
305k51576745
305k51576745
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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