SwiftUi sheet is blank on first tap but works correctly afterwards
Asked Answered
P

3

10

I have a sheet that I put outside a foreach loop . When I click on Tapgesture the first time the results are blank, after the first time I get the correct data for every row I tap . I have read Sheet inside ForEach doesn't loop over items SwiftUI but it is still not working this is my code . If you look at my code I have a state variable called sharePost which on tap gets the value of value.post then the shareSheet toggle gets set to true . For some reason I can not get the value on very first tap . Does anyone have any suggestions on fixing this issue

struct TimeLineView: View {
    @Binding var model: [MainModel]
    @Binding var isMainView: MainViewEnum
    @State var shareSheet = false
    @State var PostIDState = 0
    @State var sharePost = ""

var body: some View {
        ZStack
        {

         let result = model.sorted {
                        $0.id! > $1.id!
                    }
                    
                    ForEach(result) { value in
                   HStack {
                            Image("share")
                              .resizable()
                              .frame(width: 28, height: 28)
                              .onTapGesture {
                                            sharePost = value.post
                                            shareSheet.toggle()
                     
                                             }

                        }
                    }

                 }.sheet(isPresented: $shareSheet) 
                  {
                     ShareView(message:"\(sharePost)" )
                  }
             }

        }
Perlman answered 18/1, 2021 at 17:17 Comment(3)
Does this answer your question https://mcmap.net/q/917624/-swiftui-sheet-cannot-show-correct-values-in-first-time? (possible duplicate)Astraea
Might be related with iOS 14 changes - see iOS14 introducing errors with @State bindings. What environment do you use?Meganmeganthropus
Yes it answers my question, thank you. I am using IOS 14 .Perlman
H
13

To fix this issue, one solution is to implement the sheet modifier in the following way:

.sheet(isPresented: Binding(
                get: { shareSheet },
                set: { shareSheet = $0 }
            )) {
                ShareView(message:"\(sharePost)" )
            }
Hakan answered 31/3, 2023 at 19:53 Comment(3)
Nice, worked for me! Was trying to not use 'sheet(item' but isPresented was giving me this first attempt problem, this answer works.Delossantos
Worked for me when using fullScreenCover!Alveolus
Incredible thanks! Does this mean the default binding for sheet is simply bugged?Ember
C
7

Apparently, one of the workarounds for this is to use StateObject instead of State. Here's a Medium article that demonstrates the solution.

Cynosure answered 26/1, 2021 at 17:51 Comment(3)
can confirm this works. Thanks for the link!Rub
Yes, it works. Obviously a bug in iOS.Stratus
latest version of swiftui doesn't have @StateObjectNetherlands
S
0

Swift 5

Use this view modifier when you need to present a modal view with content from a custom data source.

.sheet(item: $sharePost, content: { sharePost in
       ShareView(message:"\(sharePost)" )
})
Slavey answered 24/4 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.