4.2 C
New York
Friday, March 21, 2025

ios – @AppStore not saving modifications when its declared in a View Mannequin


I’ve a Obeservable Class that declares two @AppStore properties:

class CaptureDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, ObservableObject {
    @AppStorage("captureInterval") var captureInterval: Int = 2  
    @AppStorage("startCaptureInterval") var startCaptureInterval: Int = 3  
}

Then I inject it into the atmosphere:

foremost
struct xxxxxx: App {

    
    @AppStorage("isOnboarding") var isOnboarding: Bool = true
    @Atmosphere(.scenePhase) var scenePhase  // Monitor app state
    @State var showingPermissionAlert: Bool = false
    @StateObject var captureDelegate = CaptureDelegate()
  
    
    var physique: some Scene {
        
        WindowGroup {
            if isOnboarding {
                OnBoardingView()
                    
            }
            else {
                CameraView()
                    .environmentObject(captureDelegate)
 
            }
            
        }

        
    }
    
    
}

I’ve a view with a customized made Wheelpicker that permits the person to regulate these two values:

struct CameraView: View {
    
    @EnvironmentObject var captureDelegate: CaptureDelegate

    TimerPickerView(captureInterval: $captureDelegate.captureInterval, startCaptureInterval: $captureDelegate.startCaptureInterval)


}

And from there I go it alongside to a ChildView:

struct TimerPickerView : View {
    
    @Binding var captureInterval: Int
    @Binding var startCaptureInterval: Int
    

    var physique: some View {
        VStack {
            // Show debug values
                      Textual content("Seize Interval: (captureInterval)")
                          .font(.caption)
            WheelPicker(rely: 10, spacing: 80, worth: $captureInterval)       //Interval Between Captures
            Textual content("Begin Seize Interval: (startCaptureInterval)")
                            .font(.caption)
            WheelPicker(rely: 20, spacing: 80, worth: $startCaptureInterval)       //Time to first seize
        }
    }
}


struct WheelPicker: View {
    
    var rely: Int      //(20 handed in)
    var spacing: CGFloat = 80
    @Binding var worth: Int
    
  .scrollPosition(id: .init(get: {    //wanted as a result of scrollPositon wont settle for a Binding int
                let place: Int? = worth
                return place //convert the binding into an optionally available Int (which scrollPosition will settle for)
            }, set: { newValue in
                    if let newValue {
                    worth = newValue    //merely taking within the new worth and go it again to the binding
                }
            }))

}

The issue is the 2 App Storage variables are initializing as 0 (as an alternative of two and three) and even once I change them efficiently within the WheelPicker it is not endured throughout launches. How do you employ @AppStorage in EnviromentObjects

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles