swiftui @state and .sheet()ios13 vs ios14
您好,我在这里遇到了一个问题,在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码对视图更新/创建订单有期望,但总的来说,它是未定义的(并且可能在iOS 14中更改)。
有明确的方法可以在表中传递信息 - 使用不同的表创建者,即。
.sheet(item:... < / code>
在此工作可靠示例。用Xcode 12 / ios进行测试14
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
这是iOS 14中的一些非常奇怪的行为,似乎没有记录在记录中。
使用其他答案在此处以及此线程,我使用
@binding
来解决问题似乎是最干净,最快的ESQ解决方案。我不知道为什么这种行为发生了变化,而且看起来比以前更直观,所以我假设它是一个错误!
一个例子:
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:
SwiftUI 2.0改变了行为,因此它也会影响MACOS 11,即使从未使用过绑定,也只需在视图上添加一个绑定即可修复它,这使我认为这是一个实现错误。
此外,仅在视图正文中使用text()中的详细信息状态变量也可以修复它。
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.