如何防止 @State 对象在视图暂时消失时被重置?

huangapple go评论91阅读模式
英文:

How do I prevent @State objects from being reset when the view temporarily disappears?

问题

  1. 我有状态对象,它们是从我的三个视图中的一个文件选择器设置的,比如在`View1`上。
  2. ```swift
  3. @State private var fileName : String = ""
  4. @State private var listFileURLs = [URL]()
  5. @State private var selectedFileIndex = 0

当我切换到另一个视图然后返回时,状态对象似乎被重置了。视图的切换是基于按钮状态的if-else子句完成的。

  1. if (showFirstView) {
  2. View1()
  3. } else if (showSecondView) {
  4. View2()
  5. }

我尝试将变量变为静态的,

  1. @State static private var fileName : String = ""
  2. @State static private var listFileURLs = [URL]()
  3. @State static private var selectedFileIndex = 0

但奇怪的是,这导致这些变量在我启动文件选择器时没有更新。

我不能将这些变量移到超级视图,因为它们是由View1中的控件设置的。
我应该怎么做?

  1. <details>
  2. <summary>英文:</summary>
  3. I have state objects which are set from a file picker on one of my three views, let&#39;s say on `View1`.

@State private var fileName : String = ""
@State private var listFileURLs = URL
@State private var selectedFileIndex = 0

  1. When I switch over to another view and return, the state objects appear to be reset. The views are switched by an if-else clause based on button states

if (showFirstView) {
View1()
} else if (showSecondView) {
View2()
}

  1. I tried turning the variables static,

@State static private var fileName : String = ""
@State static private var listFileURLs = URL
@State static private var selectedFileIndex = 0

  1. But that strangely resulted in these variables not being updated when I launched the file picker.
  2. I can&#39;t move these variables to the super View, because they are set by controls in View1.
  3. How should I proceed?
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. 可能的问题是每次更改 `showFirstView` 时都会创建一个新的视图。
  8. 您可以将 `@State` 变量从子视图移动到父视图,像这样:
  9. ```swift
  10. import SwiftUI
  11. struct ContentView: View {
  12. @State private var showingFirstView = false
  13. @State private var state1 = "文本"
  14. @State private var state2 = "文本2"
  15. var body: some View {
  16. VStack {
  17. if showingFirstView {
  18. View1(state1: $state1)
  19. } else {
  20. View2(state2: $state2)
  21. }
  22. Button("切换视图") {
  23. showingFirstView.toggle()
  24. }
  25. }
  26. .padding()
  27. }
  28. }
  29. struct View1: View {
  30. @Binding var state1: String
  31. var body: some View {
  32. VStack {
  33. TextEditor(text: $state1)
  34. }
  35. .padding()
  36. }
  37. }
  38. struct View2: View {
  39. @Binding var state2: String
  40. var body: some View {
  41. VStack {
  42. TextEditor(text: $state2)
  43. }
  44. .padding()
  45. }
  46. }
英文:

Probably the problem is that you create a new View every time showFirstView is changed.

You can move your @State var from child view to parent view like this:

  1. import SwiftUI
  2. struct ContentView: View {
  3. @State private var showingFirstView = false
  4. @State private var state1 = &quot;text&quot;
  5. @State private var state2 = &quot;text2&quot;
  6. var body: some View {
  7. VStack {
  8. if showingFirstView {
  9. View1(state1: $state1)
  10. } else {
  11. View2(state2: $state2)
  12. }
  13. Button(&quot;Switch view&quot;) {
  14. showingFirstView.toggle()
  15. }
  16. }
  17. .padding()
  18. }
  19. }
  20. struct View1: View {
  21. @Binding var state1: String
  22. var body: some View {
  23. VStack {
  24. TextEditor(text: $state1)
  25. }
  26. .padding()
  27. }
  28. }
  29. struct View2: View {
  30. @Binding var state2: String
  31. var body: some View {
  32. VStack {
  33. TextEditor(text: $state2)
  34. }
  35. .padding()
  36. }
  37. }

huangapple
  • 本文由 发表于 2023年7月6日 19:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定