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

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

3      

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

3      

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

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.