0

I'm trying to catch the tap on an item in a menu, but I can't get it to work.

I've tried something like this:

Menu("Tap me") {
    ForEach (references, id: \.self) { ref in
            Link(ref.name, destination: URL(string: ref.url))
                .simultaneousGesture(TapGesture().onEnded{
                    onLinkTapped()
                })
                .onTapGesture{
                    onLinkTapped()
                }
        }
}

but I never get to the onLinkTapped(). If it's not inside a Menu, then it works, but not inside the menu. How to catch the tap gesture?

1 Answer 1

1

Just use Button inside Menu:

Menu("Tap me") {
    ForEach (references, id: \.self) { ref in
        Button {
            onLinkTapped()
        } label: {
           Link(ref.name, destination: URL(string: ref.url))
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that works fine. I'll need to manually open the URL with UIApplication.shared.open(url, options: [:]) though, so I guess that I can just use a text as the label.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.