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

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

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

问题

我有状态对象,它们是从我的三个视图中的一个文件选择器设置的,比如在`View1`上。

```swift
@State private var fileName : String = ""
@State private var listFileURLs = [URL]()
@State private var selectedFileIndex = 0

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

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

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

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

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

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


<details>
<summary>英文:</summary>

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


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


I tried turning the variables static, 

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


But that strangely resulted in these variables not being updated when I launched the file picker.

I can&#39;t move these variables to the super View, because they are set by controls in View1. 
How should I proceed?

</details>


# 答案1
**得分**: 1

可能的问题是每次更改 `showFirstView` 时都会创建一个新的视图。

您可以将 `@State` 变量从子视图移动到父视图,像这样:

```swift
import SwiftUI

struct ContentView: View {
    @State private var showingFirstView = false
    @State private var state1 = "文本"
    @State private var state2 = "文本2"
    var body: some View {
        VStack {
            if showingFirstView {
                View1(state1: $state1)
            } else {
                View2(state2: $state2)
            }
            Button("切换视图") {
                showingFirstView.toggle()
            }
        }
        .padding()
    }
}

struct View1: View {
    @Binding var state1: String
    var body: some View {
        VStack {
            TextEditor(text: $state1)
        }
        .padding()
    }
}

struct View2: View {
    @Binding var state2: String
    var body: some View {
        VStack {
            TextEditor(text: $state2)
        }
        .padding()
    }
}
英文:

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:

import SwiftUI

struct ContentView: View {
    @State private var showingFirstView = false
    @State private var state1 = &quot;text&quot;
    @State private var state2 = &quot;text2&quot;
    var body: some View {
        VStack {
            if showingFirstView {
                View1(state1: $state1)
            } else {
                View2(state2: $state2)
            }
            Button(&quot;Switch view&quot;) {
                showingFirstView.toggle()
            }
        }
        .padding()
    }
}

struct View1: View {
    @Binding var state1: String
    var body: some View {
        VStack {
            TextEditor(text: $state1)
        }
        .padding()
    }
}

struct View2: View {
    @Binding var state2: String
    var body: some View {
        VStack {
            TextEditor(text: $state2)
        }
        .padding()
    }
}

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:

确定