Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swiftui how to catch taps on menu item

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading