SwiftUI view execution confusion, both buttons execute same code

I am fairly new to SwiftUI and am having trouble understanding why the following view / form executes the same code no matter what button is pushed. If I click the cancel button I get on the console.

in cancel
in save

If I click the save button, I get the same thing.

in cancel
in save

I know I am missing something fundamental, but can't sort it out. Any help would be appreciated.

    var body: some View {
        Form {
            TextField("Enter name", text: $personModel.modelName)
            HStack {
                Button("Cancel") {
                    print("in cancel")
                    presentationMode.wrappedValue.dismiss()
                }
                Spacer()
                Button("Save") {
                    print("in save")
                    personModel.save()
                    presentationMode.wrappedValue.dismiss()
                }
            }
        }
    }
Answered by Claude31 in 692235022

Change like this:

    var body: some View {
        Form {
            TextField("Enter name", text: $personModel.modelName)
            HStack {
                Button("Cancel") {
                    print("in cancel")
                    presentationMode.wrappedValue.dismiss()
                }
                .buttonStyle(BorderlessButtonStyle())
                Spacer()
                Button("Save") {
                    print("in save")
                    personModel.save()
                    presentationMode.wrappedValue.dismiss()
                }
               .buttonStyle(BorderlessButtonStyle())
            }
        }
    }

Adding .buttonStyle(BorderlessButtonStyle()).

Problem was caused by Form (idem with List) : all the HStack react to tap.

Read here:

h t t p s : / / w w w.hackingwithswift.com/forums/swiftui/button-s-on-click-event-being-applied-to-hstack-surrounding-it/2859

Accepted Answer

Change like this:

    var body: some View {
        Form {
            TextField("Enter name", text: $personModel.modelName)
            HStack {
                Button("Cancel") {
                    print("in cancel")
                    presentationMode.wrappedValue.dismiss()
                }
                .buttonStyle(BorderlessButtonStyle())
                Spacer()
                Button("Save") {
                    print("in save")
                    personModel.save()
                    presentationMode.wrappedValue.dismiss()
                }
               .buttonStyle(BorderlessButtonStyle())
            }
        }
    }

Adding .buttonStyle(BorderlessButtonStyle()).

Problem was caused by Form (idem with List) : all the HStack react to tap.

Read here:

h t t p s : / / w w w.hackingwithswift.com/forums/swiftui/button-s-on-click-event-being-applied-to-hstack-surrounding-it/2859

I'm seeing similar behavior in iOS 15. I have several buttons in an hstack. Applying button styles to the buttons makes no difference. What does happen is that some button touches will be accepted, but then the buttons stop responding. If I tap on a button on another part of the view, then those buttons are able to function again. The behavior only shows up on iOS 15, on iOS 14, the buttons behave correctly.

SwiftUI view execution confusion, both buttons execute same code
 
 
Q