UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: .sheet or .fullSheetCover: both 'cancel' button and 'save' button are in fact running the code in my 'save' action

Forums > SwiftUI

Hi, I'm having trouble dismissing a sheet/fullSheetCover (I've tried both). I have a 'cancel' button and a 'save' button - but whichever button I press it appears to run the 'save' button. I can tell this because in the parent view my list of meds has increased by one, even if I press cancel. Ideas?

struct StatDrugAddView: View {
    //@Environment(\.dismiss) var dismiss
    @ObservedObject var statMeds: StatMeds
    @Binding var sheetIsPresented: Bool

    @State private var route = ""
    @State private var drugName = ""
    @State private var dose = ""
    @State private var units = ""

    var body: some View {
        Form {
            HStack {
                Text("Name")
                TextField("Drug name", text: $drugName)
                    .textfieldModifiers()
            }

            HStack {
                Text("Dose")
                TextField("Dose", text: $dose)
                    .textfieldModifiers()
                //.keyboardType(.numberPad)

            }

            HStack {
                Text("Units")
                TextField("Units", text: $units)
                    .textfieldModifiers()
            }

            HStack {
                Text("Route")
                TextField("Administration route", text: $route)
                    .textfieldModifiers()
            }

            HStack {
                Spacer()

                Button("Cancel") {
                    //dismiss()
                    sheetIsPresented = false
                }

                Spacer()

                Button("Add") {
                    let drug = StatMed(name: drugName, dose: dose, units: units, route: route)
                    statMeds.drugs.append(drug)
                    sheetIsPresented = false
                }

                Spacer()
            }
        }
    }

3      

You need to show the code for your parent view, and how the StatDrugAddView is being called.

You don't have a save button here, but do have the cancel and add button, with the add button appending to statMeds.

3      

Yeah, sorry, when I said 'save', I meant 'add'. Tired when I posted that. When adding, the medication is meant to append to StatMeds, cancel is not meant to append, but it still appends with the properties all being "". I don't think showing the parent view will help as the problem is with this view, surely? I'm calling it with .sheet(isPresented: ) and have tried .fullSheetCover(isPresented: ) too. I did not enter anything for onDismiss as it made more sense to me to dismiss from the view itself?

3      

It's from the buttons being in an HStack inside a Form.

The solution is to do this:

HStack {
    Spacer()

    Button("Save") {
        //button action code
    }
    .buttonStyle(.borderless) // <-- add this line
    //or pre-iOS15: .buttonStyle(BorderlessButtonStyle())

    Spacer()

    Button("Add") {
        //button action code
    }
    .buttonStyle(.borderless) // <-- add this line
    //or pre-iOS15: .buttonStyle(BorderlessButtonStyle())

    Spacer()
}

3      

@roosterboy, you always come up with the solution for me, thank you! Such a random solution, but it works, thanks so much!!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.