swiftui @state and .sheet()ios13 vs ios14

发布于 2025-02-10 21:55:21 字数 682 浏览 2 评论 0原文

您好,我在这里遇到了一个问题,在iOS13或ios14上运行时,我在.sheet()视图之间没有一致的行为,

我得到了这样的视图:

@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)
       }
 }

在iOS 13上,这项工作如预期的btn click->设置标签 - >呼叫表 - >在文本视图中显示“标签”。

在ios14上,我在self.label中获得了一个空字符串。

我错过了什么吗?是iOS 14错误还是我在iOS13上误解了,然后得到了纠正。

PS:我还有其他几个变量在闭合中通过了,我简化了它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残花月 2025-02-17 21:55:21

您的代码对视图更新/创建订单有期望,但总的来说,它是未定义的(并且可能在iOS 14中更改)。

有明确的方法可以在表中传递信息 - 使用不同的表创建者,即。 .sheet(item:... < / code>

在此工作可靠示例。用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)
        }
    }
}

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)
        }
    }
}
冷…雨湿花 2025-02-17 21:55:21

这是iOS 14中的一些非常奇怪的行为,似乎没有记录在记录中。

使用其他答案在此处以及此线程,我使用@binding来解决问题似乎是最干净,最快的ESQ解决方案。

我不知道为什么这种行为发生了变化,而且看起来比以前更直观,所以我假设它是一个错误!

一个例子:

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)
    }
}

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)
    }
}
大海や 2025-02-17 21:55:21

SwiftUI 2.0改变了行为,因此它也会影响MACOS 11,即使从未使用过绑定,也只需在视图上添加一个绑定即可修复它,这使我认为这是一个实现错误。
此外,仅在视图正文中使用text()中的详细信息状态变量也可以修复它。

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 ?? "") }
        }
    }
}

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 ?? "") }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文