简体   繁体   中英

Bind to a struct property in a SwiftUI Mac app

I'm building a macOS unit converter app in SwiftUI. In the example below, I have an acceleration view that has many text fields. These text fields represent different units of acceleration.

// AccelerationView.swift

struct AccelerationView: View {

    @State private var accel = Acceleration()
    @EnvironmentObject var formatter: Formatter

    var body: some View {

        VStack(alignment: .leading) {
            Text("Metric").font(.subheadline)
            HStack {
                TextField("kilometer per second squared", value: $accel.kilometerPerSecondSquare, formatter: formatter.numberFormatter).frame(width: 120)
                Text("km/s²").frame(width: 60, alignment: .leading)
            }
            HStack {
                TextField("meter per second squared", value: $accel.meterPerSecondSquare, formatter: formatter.numberFormatter).frame(width: 120)
                Text("m/s²").frame(width: 60, alignment: .leading)
            }
            HStack {
                TextField("millimeter per second squared", value: $accel.millimeterPerSecondSquare, formatter: formatter.numberFormatter).frame(width: 120)
                Text("mm/s²").frame(width: 60, alignment: .leading)
            }
            // and so on ...
        }
        .padding()

    }
}

I would like to place the HStack into its own file to clean up the code in the view. My attempt at this is shown below:

// UnitTextField.swift

struct UnitTextField: View {

    @State var value: Double = 0.0
    let placeHolder: String
    let label: String
    var fieldWidth: CGFloat = 120
    var labelWidth: CGFloat = 60

    @EnvironmentObject private var formatter: Formatter

    var body: some View {
        HStack {
            TextField(placeHolder, value: $value, formatter: formatter.numberFormatter)
                .frame(width: fieldWidth)
                .multilineTextAlignment(.trailing)
            Text(label)
                .frame(width: labelWidth, alignment: .leading)
        }
    }
}

This does not work because the UnitTextField value does not properly bind to the acceleration struct. I'm trying to accomplish something like this that I can use in the AccelerationView:

// AccelerationView.swift

struct AccelerationView: View {

    @State private var accel = Acceleration()
    @EnvironmentObject var formatter: Formatter

    var body: some View {

        VStack(alignment: .leading) {
            Text("Metric").font(.subheadline)
            UnitTextField(value: $accel.kilometerPerSecondSquare, placeHolder: "kilometer per second squared", label: "km/s²")
            UnitTextField(value: $accel.meterPerSecondSquare, placeHolder: "meter per second squared", label: "m/s²")
            UnitTextField(value: $accel.millimeterPerSecondSquare, placeHolder: "millimeter per second squared", label: "mm/s²")
            // and so on ...
        }
        .padding()
    }
}

Any suggestions on how to properly implement this in SwiftUI for a macOS application?

If to go from what you're going to accomplish (last snapshot), then I assume you need

struct UnitTextField: View {
    @Binding var value: Double

instead of

struct UnitTextField: View {
    @State var value: Double = 0.0

It could be like this with binding and environmentObject :

 class MyFormat: Formatter, ObservableObject{
                var numberFormat = NumberFormatter()
            }


            struct UnitTextField: View {

                @Binding var value: Double
                let placeHolder: String
                let label: String
                var fieldWidth: CGFloat = 120
                var labelWidth: CGFloat = 60

                @EnvironmentObject  var formatter: MyFormat

                var body: some View {
                    HStack {
                        TextField(placeHolder, value: $value, formatter: formatter.numberFormat)
                            .frame(width: fieldWidth)
                            .multilineTextAlignment(.trailing)
                        Text(label)
                            .frame(width: labelWidth, alignment: .leading)
                    }
                }
            }

            struct Acceleration{
                var kilometerPerSecondSquare : Double = 0.0
                var meterPerSecondSquare : Double = 1.0
                var millimeterPerSecondSquare : Double = 2.0

            }


            struct AccelerationView: View {

                @State private var accel = Acceleration()
                @EnvironmentObject var formatter: MyFormat

                var body: some View {

                    VStack(alignment: .leading) {
                        Text("Metric").font(.subheadline)
                        UnitTextField(value: $accel.kilometerPerSecondSquare, placeHolder: "kilometer per second squared", label: "km/s²").environmentObject(formatter)
                        UnitTextField(value: $accel.meterPerSecondSquare, placeHolder: "meter per second squared", label: "m/s²").environmentObject(formatter)
                        UnitTextField(value: $accel.millimeterPerSecondSquare, placeHolder: "millimeter per second squared", label: "mm/s²").environmentObject(formatter)
                        // and so on ...
                    }
                    .padding()
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM