1

I have a list which is already containing different values. For each List postitions I have made a button which leads to a sheet View which contains a Picker to change the value of minutes and seconds. When I open up this Sheet view it will present for each Listrow the right amount, but if I change it, it will not be passed to the row once i close the sheetview.

It will also show me the Warning on both picker values "$minutes" and "$seconds"

What am I doing wrong? What is missing?

import SwiftUI

struct LevelData: Identifiable {
    let id = UUID()
    let imageName: String
    let title: String
    @State var minutes: Int
    @State var seconds: Int
}

struct IntervalListView: View {
    
    static let Level4Interval1Medium = [
    LevelData(imageName: "BreatheSymbol", title: "Breathe", minutes: 2, seconds: 00),
    LevelData(imageName: "HoldsSymbol", title: "Hold", minutes: 2, seconds: 00),
    LevelData(imageName: "BreatheSymbol", title: "Breathe", minutes: 1, seconds: 45),
    LevelData(imageName: "HoldsSymbol", title: "Hold", minutes: 2, seconds: 00),
    ]
    
    @State var LevelDatas: [LevelData] = Level4Interval1Medium
    @State private var showingSheet = false
    
   
    
    var body: some View {
        
        ZStack {
            VStack {
                
                List {
                    Section{
                       
                        HStack {
                            Text("14 min")
                            
                            
                                Spacer()
                            NavigationLink("Start", destination: SomeView())
                                .frame(width: 60.0)
                                .foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
                            Spacer()
                            Image("Save")
                                
                        }.listRowBackground(Color(.secondarySystemBackground))
                    }
                    
                    ForEach(LevelDatas,id: \.id) {
                        item in
                        
                        HStack {
                        
                            Button("") {
                                showingSheet.toggle()
                            }.sheet(isPresented: $showingSheet) {
                                ZStack{
                                    
                                    VStack(spacing: 20) {
                                        Text("Set \(item.title) Time")
                                            .bold().padding()
                                            .frame(maxWidth: .infinity)
                                            .background(Color(#colorLiteral(red: 0, green: 0.22, blue: 0.36, alpha: 1)))
                                            .foregroundColor(Color.white)
Spacer()
                                        HStack {
                                            
                                            VStack {
                                                Picker("", selection: item.$minutes){
                                                    ForEach(0..<9, id: \.self) { i in
                                                        Text("\(i) min").padding(.leading, 40.0).tag(i)
                                                    }
                                                }
                                                
                                                .pickerStyle(WheelPickerStyle())
                                                
                                                .clipped()
                                            }

                                            VStack {
                                                Picker("", selection: item.$seconds){
                                                    ForEach(0..<60, id: \.self) { i in
                                                        
                                                        Text("\(i) sec").padding(.trailing, 40.0).tag(i)
                                                    }
                                                }
                                                .pickerStyle(WheelPickerStyle())
                                                
                                                .clipped()
                                            }
                                            
                                        }

                                        Spacer()

                                        Button(action: {
                                            self.showingSheet = false
                                        }){
                                            Text("Done")
                                        } .padding()
                                    }
                                    
                                } .presentationDetents([.medium, .large])
                            }
                            
                             
                            Image(item.imageName)
                                
                            Text (item.title)
                                .font(.title2)
                                .fontWeight(.semibold)
                            Spacer()
                            Text("\(item.minutes):\(item.seconds < 10 ? "0" : "")\(item.seconds)")
                                .padding(.trailing, 10.0)
                            
                      
                        }
                        
                    }.onDelete { indexSet in
                        LevelDatas.remove(atOffsets: indexSet)
                    }
                    
                    
                }.navigationTitle("Overview Intervals")
                    .toolbar {
                        EditButton()
                    }
                    
                
            }
        }
        
        
        
    }
    
    struct IntervalListView_Previews: PreviewProvider {
        static var previews: some View {
            IntervalListView()
        }
    }
}
2
  • Note; you should not use @State var in your struct LevelData, @State is only for use in Views. Have a look at this link, it gives you examples of how to manage data in your app using ObservableObject: developer.apple.com/documentation/swiftui/… Commented Oct 13, 2022 at 0:29
  • It's best you spend sometime learning the basics of SwiftUI before diving into building an app would save you a lot of time in the long run. WWDC has a lot of great content .. go step by step .. understand views, data flow, etc Commented Oct 13, 2022 at 17:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.