SwiftUI Sheet not updating variable

Related searches

I'm trying to pass an item to a sheet, if I change the values by selecting another item. The sheet keeps displaying the first item, even when I never called the sheet with the first item.

Is this a bug or am I doing something wrong?

WorkoutList.swift

import SwiftUI

struct WorkoutList: View {
    private var workouts = workoutData
    @State var showWorkoutDetails = false

    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 20) {
                ForEach(workouts) { workoutCard in
                    GeometryReader { geometry in
                        Button(action: {self.showWorkoutDetails.toggle()}) {
                            WorkoutCardView(workout: workoutCard)
                                .rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
                                .sheet(isPresented: self.$showWorkoutDetails) {
                                    WorkoutDetails(workout: workoutCard)
                                }
                        }
                    }
                    .frame(width:246, height: 360)

                }
            }
        .padding(30)
        }
    }
}

private let workoutData = [
    Workout(name: "Workout #1", imageName: "workoutImage", color: Color.orange),
    Workout(name: "Workout #2", imageName: "workoutImage", color: Color.green),
    Workout(name: "Workout #3", imageName: "workoutImage", color: Color.red),
    Workout(name: "Workout #4", imageName: "workoutImage", color: Color.blue)
]

WorkoutDetails.swift

import SwiftUI

struct WorkoutDetails: View {
    var workout = Workout(name: "", imageName: "", color: Color.black)

    var body: some View {
        GeometryReader { geometry in
            ZStack {
                Rectangle()
                    .fill(self.workout.color)

                VStack(alignment: .leading) {
                    VStack(alignment: .leading) {
                        Text(self.workout.name)
                            .foregroundColor(.white)
                            .font(.title)
                            .fontWeight(.heavy)
                            .lineLimit(nil)
                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                            .frame(height: 70)
                    }
                    .frame(width: 180)

                    Image(self.workout.imageName)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: geometry.size.width-60, height: 200)
                        .padding()

                    Spacer()
                }
                .padding(.top, 40)
                .padding(.leading, 10)
            }
        }
        .edgesIgnoringSafeArea(.bottom)
    }
}

Here I want to display a workout. But whatever I change it, I always get "workout #1" and I don't know what I'm doing wrong.

Create a binding with an optional workout. Nil will be the case that the sheet is not presented, assigning a workout to this variable displays the corresponding sheet.

struct WorkoutList: View {
  private var workouts = workoutData
  @State var selectedWorkout: Workout?

  var body: some View {
    ScrollView(.horizontal, showsIndicators: false) {
      HStack(spacing: 20) {
        ForEach(workouts) { workoutCard in
          GeometryReader { geometry in
            Button(action: { self.selectedWorkout = workoutCard }) {
              WorkoutCardView(workout: workoutCard).rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0)).sheet(item: self.$selectedWorkout) { workout in
                WorkoutDetails(workout: workout)
              }
            }
          }.frame(width:246, height: 360) 
        }
      }.padding(30)
    }
  }
}

SwiftUI Sheet not updating variable, a sheet, if I change the values by selecting another item. The sheet keeps displaying SwiftUI Sheet not updating variable. 15 Views. 0. Votes. Within your ContentView, initialize your variable with @ObservedObjectwhich will watch for any announcements. Without @ObservedObject, the change announcements would be sent but ignored. This changes will also be reflected on all the screens knowing that this will also update the UI with the help of SwiftUI. @ObservedObject var user = User()

I had similar problem. You want to assign workoutCard to @State and inside .sheet have some logic that decides what view to show

Here is more on your problem: Multiple sheet(isPresented:) doesn't work in SwiftUI

Why is my child SwiftUI view not updating?, So I suspect that making ChildView be a POD (Plain Old Data) type, with no @ State variables, would in fact cause it to redraw, since SwiftUI has no other means� If you’ve been using SwiftUI for a while now, you probably hit the problem where you find yourself trying to update the state of a view from inside its body. Usually, Xcode complains during runtime. When it does, you are forced to put your update inside a DispatchQueue closure (not feeling too good about yourself) but you carry on anyway.

Basically we need to store selectedWorkout when the button tapped.

@State var isDetailPresented = false
@State var selectedWorkout: Workout?

...

Button(action: {
    self.selectedWorkout = workoutCard
    self.isDetailPresented.toggle()
}, label: {
    //Your view here
}).buttonStyle(PlainButtonStyle())
  .sheet(isPresented: $isDetailPresented) {
    WorkoutDetails(workout: self.selectedWorkout!)
}
...

How to modify state from state in SwiftUI, Modifying state during view update, this will cause undefined behavior. Note that we use $ prefix from a variable to form Binding import SwiftUI .sheet( isPresented: $showImagePicker, content: { compiles, but after we pick an image, which should change the image property of our viewModel , the view is not reloaded. Updated for Xcode 12.0. SwiftUI’s sheets are used to present new view controllers modally over existing ones, like calling present() on a UIViewController in UIKit. To use one, give it something to show (some text, an image, a custom view, etc), add a Boolean that defines whether the detail view should be showing, then attach it to your main view as a modal sheet.

SOLVED: Use variable of one class in another class – SwiftUI , I have a View that is shown in a sheet to fill in the User Preferences : However, it's a class, not a View and if I try to write following in my� Updated for Xcode 12.0. For data that should be shared with all views in your entire app, SwiftUI gives us @EnvironmentObject.This lets us share model data anywhere it’s needed, while also ensuring that our views automatically stay updated when that data changes.

Published variable not changing : SwiftUI, Published variable not changing. Question. I have this But when i do end up searching for something my variable holding all the locations is not updated. But all the information presentationMode handles the sheet being shown or hidden . UPDATE: You should not call Network request in the init method of the ViewModel as mentioned by Michael Long. I recorded the video to show the problem and how to fix it. You can watch the video

Updated for Xcode 12.0. SwiftUI gives us equivalents to UIKit’s viewDidAppear() and viewDidDisappear() in the form of onAppear() and onDisappear().You can attach any code to these two events that you want, and SwiftUI will execute them when they occur.