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

SOLVED: Passing @State from parent onLongPressGesture to child view

Forums > SwiftUI

I keep getting a fatal error when unwrapping @State property that contains an optional. I was able to boil down the code to this:

import SwiftUI

struct FoodResult: Identifiable {
    var id: String { name }
    var name: String
    var isFavorite: Bool
}

struct ParentView: View {
    @State private var onFoodLongClicked = false
    @State private var clickedFood: FoodResult? = nil

    var body: some View {
        VStack{
            List(loadData()) { item in
                Text(item.name)
                    .onLongPressGesture {
                        clickedFood = item
                        onFoodLongClicked = true
                    }
            }
        }.sheet(isPresented: $onFoodLongClicked) {
            ChildView(showPicker: $onFoodLongClicked, food: Binding($clickedFood)!) //  <-- FATAL ERROR HERE
        }
    }    

    func loadData() -> [FoodResult] {
        return [
            FoodResult(name: "test1", isFavorite: false),
            FoodResult(name: "test2", isFavorite: false),
            FoodResult(name: "test3", isFavorite: true)
        ]
    }
}

struct ChildView: View {    
    @Binding var showPicker: Bool
    @Binding var food: FoodResult

    var body: some View {
        Text(food.name)
            .onTapGesture { 
                showPicker = false
            }
    }
}

What I want to achieve is to display a child view inside a sheet (or any other popup alike view) when a user long presses one of the list items.

1      

You could try this:

import SwiftUI

struct FoodResult: Identifiable {
    var id: String { name }
    var name: String
    var isFavorite: Bool
}

struct ParentView: View {
    @State private var onFoodLongClicked = false
    @State private var clickedFood: FoodResult? = nil
    @State private var otherItem = 0

    var body: some View {
        VStack{
            List(loadData()) { item in
                Text(item.name)
                    .onLongPressGesture {
                        clickedFood = item
                        otherItem += 1
                    }
                    .onChange(of: otherItem) { _ in
                        onFoodLongClicked = true
                    }
            }
        }.sheet(isPresented: $onFoodLongClicked) {
            ChildView(showPicker: $onFoodLongClicked, food: Binding($clickedFood)!) //  <-- FATAL ERROR HERE
        }
    }

    func loadData() -> [FoodResult] {
        return [
            FoodResult(name: "test1", isFavorite: false),
            FoodResult(name: "test2", isFavorite: false),
            FoodResult(name: "test3", isFavorite: true)
        ]
    }
}

struct ChildView: View {
    @Binding var showPicker: Bool
    @Binding var food: FoodResult

    var body: some View {
        Text(food.name)
            .onTapGesture {
                showPicker = false
            }
    }
}

It's seems similar to a problem I had here:

https://www.hackingwithswift.com/forums/swiftui/passing-a-boolean-from-one-view-to-the-next/12023/12032

1      

What I want to achieve is to display a child view inside a sheet (or any other popup alike view) when a user long presses one of the list items.

Are you just wanting to display the info from the clicked FoodResult or do you intend on allowing editing of that info?

If all you are doing is displaying it (which is what I will assume given what you wrote), then the solution is easy. Use this .sheet() modifier instead:

.sheet(item: $clickedFood) { //takes a Binding to an Optional
    //code here will run when the sheet is dismissed
} content: { food in //a non-Optional is passed into the sheet
    ChildView(food: food)
}

And for ChildView, you don't need the showPicker binding in order to dismiss the sheet:

struct ChildView: View {
    @Environment(\.dismiss) var dismiss

    let food: FoodResult

    var body: some View {
        Text(food.name)
            .onTapGesture {
                dismiss()
            }
    }
}

2      

Thanks for pointing this out @roosterboy. Your example works fine for displaying the tapped item which was what I asked. My original intention was to long tap an item so I can enter edit mode of that item. Can you also share a code snippet on how to can I update the long tapped item from the child view and have the changes reflected in the parent view?

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.