SwiftUI @State and .sheet() ios13 vs ios14

[亡魂溺海] 提交于 2021-02-03 12:44:10

问题


Hello I am running into a problem here and I do not have a consistent behavior between my .sheet() view when running on ios13 or ios14

I got a view like this :

@State private var label: String = "" 
@State private var sheetDisplayed = false
///Some code
var body: some View {
   VStack {
      Button(action: {
         self.label = "A label"
         self.isDisplayed = true
      }) {
           Text("test")
       }
   }.sheet(isPresented: $sheetDisplayed, onDismiss: {
        self.label = ""
    }) {
        Text(self.label)
       }
 }

On ios 13 this work as expected btn click -> set label -> call sheet -> display "A label" in a Text view.

On ios14 I got an empty string in self.label when in sheet closure, hence it does not display anything.

Did I missed something ? Is it an iOS 14 bug or did I had it wrong on ios13 and that got corrected.

PS: I have a couple of other variables that are passed in the closure I simplified it.


回答1:


Your code have expectation of view update/creation order, but in general it is undefined (and probably changed in iOS 14).

There is explicit way to pass information inside sheet - use different sheet creator, ie. .sheet(item:...

Here is working reliable example. Tested with Xcode 12 / iOS 14

struct ContentView: View {
    @State private var item: Item?

    struct Item: Identifiable {
        let id = UUID()
        var label: String = ""
    }

    var body: some View {
        VStack {
            Button(action: {
                self.item = Item(label: "A label")
            }) {
                Text("test")
            }
        }.sheet(item: $item, onDismiss: {
            self.item = nil
        }) {
            Text($0.label)
        }
    }
}



回答2:


This is some really strange behaviour in iOS 14, which doesn't appear to be documented.

Using the other answer here and the comment on this thread, I used @Binding to solve the issue as it seemed the cleanest and most SwiftUI-esq solution.

I have no idea why this behaviour has changed, and it seems less intuitive than before, so I'm assuming its a bug!

An example:

struct MainView: View {
    @State private var message = ""
    @State private var showSheet = false

    var body: some View {
        Button(action: {
            self.message = "This will display the correct message"
            self.showSheet = true
        }, label: {
            Text("Test Button")
        })
        .sheet(isPresented: self.$showSheet) {
            SheetView(message: self.$message)
        }
    }
}

struct SheetView: View {
    @Binding var message: Int

    var body: some View {
        Text(self.message)
    }
}



回答3:


The behaviour changed with SwiftUI 2.0, so it affects MacOS 11 as well, just adding a binding to the view fixes it even when that binding is never used, which makes me think this is an implementation bug. Additionally just using the details state variable in a Text() within the body of the view also fixes it.

struct MyViewController : View {

    @State var details: String?
    @State var showDetails = false

    //    @Binding var havingAbindingFixesIt: String?

    var body: some View {
        VStack {
            //            Text(details ?? "")
            Text("Tap here for details")
                .onTapGesture {
                    self.details = "These are the details"
                    self.showDetails.toggle()
                }
                .sheet(isPresented: $showDetails) { Text(details ?? "") }
        }
    }
}


来源:https://stackoverflow.com/questions/63946914/swiftui-state-and-sheet-ios13-vs-ios14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!