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()
                }
            }
        }
    }

Accepted Reply

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

  • Thanks Claude31 that worked great, but how does changing the button style change the execution? I thought styles were more to do with the look and feel of the button, but didn't realize it would effect the execution. Or is just the fact that there was a buttonstyle at all. Just trying to figure out why it worked, so I can use it in other situations.

    Thanks

  • Yes, that’s one of the many bizarre behaviors of SwiftUI (and why I do not like it so much) to confuse between View and Control (good old MVC). Here, I guess that applying a style sets de facto a frame to the button. I will test just adding a frame to buttons, to check hypothesis. Good continuation,

Add a Comment

Replies

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

  • Thanks Claude31 that worked great, but how does changing the button style change the execution? I thought styles were more to do with the look and feel of the button, but didn't realize it would effect the execution. Or is just the fact that there was a buttonstyle at all. Just trying to figure out why it worked, so I can use it in other situations.

    Thanks

  • Yes, that’s one of the many bizarre behaviors of SwiftUI (and why I do not like it so much) to confuse between View and Control (good old MVC). Here, I guess that applying a style sets de facto a frame to the button. I will test just adding a frame to buttons, to check hypothesis. Good continuation,

Add a Comment

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.