Home > Enterprise >  Is there a way to solve the no-data when I click on the view for the first time? In Swift
Is there a way to solve the no-data when I click on the view for the first time? In Swift

Time:11-02

I want to display the content of a Card with a Modal View that appears when I click on the Custom Card, but when I click only one card (not different card) , it displays a Model View with no data in it. After that, it works good for every card. This is the code where the error is:

import SwiftUI


struct GoalsView: View {
    
    @Binding var rootActive: Bool
    @State private var showSheet = false
    @Binding var addGoalIsClicked: Bool

    @State private var selectedGoal : Goal = Goal(title: "", description: "")
    
    var body: some View {
        
        ScrollView {
            VStack(){
                HStack{
                    Text("Add goals")
                        .fontWeight(.bold)
                        .font(.system(size: 28))
                    Spacer()
                }
                .padding()
                
                ForEach(goalDB) {goal in
                    Button() {
                        selectedGoal = goal
                        showSheet = true
                    }label: {
                        Label(goal.title, systemImage: "")
                            .frame(maxWidth: .infinity)
                            .foregroundColor(.black)
                    }
                    .padding()
                    .fullScreenCover(isPresented: $showSheet) {
                        ModalView(rootActive: $rootActive, goal: selectedGoal)
                    }
                    .background {
                        RoundedRectangle(cornerRadius: 10)
                            .frame(maxWidth: .infinity)
                            .foregroundColor(.white)
                }
                }
                .padding(4)
                
            }
        }.background(Color.gray.opacity(0.15))
     
    }
}


I tried to find a solution on the Internet (someone has my same problem), reading the documentation of the FullScreenCover but I can not find out how to do it

CodePudding user response:

Instead of using a Boolean to control whether or not to show your sheet/full screen cover (I'm going to use "sheet" throuhought, the principle is identical), you can instead use an optional object.

In this scenario, you start with an optional value that is nil. When you want your sheet to display, you set the object to an actual value. Dismissing the sheet will return your state object to nil, just as dismissing a sheet managed by a Boolean resets the state to false.

In your code, you can remove your showSheet state value, and convert selectedGoal to an optional:

@State private var selectedGoal: Goal?

Your button action now only needs to set this value:

Button {
  selectedGoal = goal
} label: {
  // ...
}

And then in your modifier, you use the item: form instead. The closure receives the unwrapped version of the state object – i.e., it'll always get a Goal, rather than a Goal?. This is the value you should pass into the view being presented:

.fullScreenCover(item: $selectedGoal) { goal in
  ModalView(rootActive: $rootActive, goal: goal)
}
  • Related