NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: passing parameter is displaying the previous content of an EnvironmentObject

Forums > SwiftUI

this is a Macos app where the parsclass is setup in a previous view that contains the YardageRowView below. That previous view is responsible for changing the contents of the parsclass. This is working is other views that use a NavigationLink to display the views.

When the parsclass is changed, this view is refreshed, but the previous value is put in the text field on the holeValueTextView. This is the output of the logs being done

  • ** YardageRowView
  • ** Before 110
  • ** After 110
  • ** holeValueTestView 337
  • ** Before 222
  • ** After 222
  • ** holeValueTestView 347

The Before 110 and After 110 are correct...the holeValueTestView 337 is from the previous content of the parsclass and had been displayed already before the parsclass was changed

I cannot comprehend how the value is not being passed into the holeValueTestView correctly

This is a view shown as a .sheet, and if I dismiss it and display it again, everything is fine.

struct YardageRowView: View {

    @EnvironmentObject var parsclass: parsClass

    var body: some View {
        HStack(spacing: 0) {
            myLog("YardageRowView ")
            Text("Yards").frame(width: holeDimensions().titleColumnWidth)
            ForEach(parsclass.pars.indices, id: \.self) { indice in
                myLog("Before \(String(parsclass.pars[indice].Yardage))")

                // this displays the previous value 337
                holeValueTestView(value: String(parsclass.pars[indice].Yardage))
                myLog("After  \(String(parsclass.pars[indice].Yardage))")

                // this displays the current value 110
                Text(String(parsclass.pars[indice].Yardage))
                    .foregroundColor(.red)
            }
        }
    }
struct holeValueTestView: View {
    @State  var value: String

    var body: some View {
    //return TextField(value, text: $value)
        myLog("holeValueTestView \(value)")
         Text(value)
    }
}

   

Don't use @State in your holeValueTestView. Change it to an @Binding or a let.

   

Thanks, used a var and it is working fine, so obvious. Yea for a second set of eyes

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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

Reply to this topic…

You need to create an account or log in to reply.

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.