@State var 不存储值
@State var doesn't store value
我的目标是在视图之间传递值,从 Chooser 到 ThemeEditor。当用户按下一个图标时,我将保存我想要传递的对象,稍后使用 sheet
并传递包含 @State var
.
内容的新创建的视图
已成功分配给 @State var themeToEdit
,但是在 sheet
中创建 ThemeEditor
视图时它是 nil
我做错了什么?
struct Chooser: View {
@EnvironmentObject var store: Store
@State private var showThemeEditor = false
@State private var themeToEdit: ThemeContent?
@State private var editMode: EditMode = .inactive
@State private var isValid = false
var body: some View {
NavigationView {
List {
ForEach(self.store.games) { game in
NavigationLink(destination: gameView(game))
{
Image(systemName: "wrench.fill")
.imageScale(.large)
.opacity(editMode.isEditing ? 1 : 0)
.onTapGesture {
self.showThemeEditor = true
/* themeInfo is of type struct ThemeContent: Codable, Hashable, Identifiable */
self.themeToEdit = game.themeInfo
}
VStack (alignment: .leading) {
Text(self.store.name(for: something))
HStack{
/* some stuff */
Text(" of: ")
Text("Interesting info")
}
}
}
}
.sheet(isPresented: $showThemeEditor) {
if self.themeToEdit != nil { /* << themeToEdit is nil here - always */
ThemeEditor(forTheme: self.themeToEdit!, $isValid)
}
}
}
.environment(\.editMode, $editMode)
}
}
}
struct ThemeEditor: View {
@State private var newTheme: ThemeContent
@Binding var isValid: Bool
@State private var themeName = ""
init(forTheme theme: ThemeContent, isValid: Binding<Bool>) {
self._newTheme = State(wrappedValue: theme)
self._validThemeEdited = isValid
}
var body: some View {
....
}
}
struct ThemeContent: Codable, Hashable, Identifiable {
/* stores simple typed variables of information */
}
.sheet
内容视图是在创建时捕获的,所以如果你想检查里面的东西,你需要使用.sheet(item:)
变体,比如
.sheet(item: self.$themeToEdit) { item in
if item != nil {
ThemeEditor(forTheme: item!, $isValid)
}
}
注意:不清楚什么是 ThemeContent
,但可能需要使其符合其他协议。
使用Binding
。用这个改变你的 ThemeEditor 视图。
struct ThemeEditor: View {
@Binding private var newTheme: ThemeContent?
@Binding var isValid: Bool
@State private var themeName = ""
init(forTheme theme: Binding<ThemeContent?>, isValid: Binding<Bool>) {
self._newTheme = theme
self._isValid = isValid
}
var body: some View {
....
}
}
以及 sheet 代码
.sheet(isPresented: $showThemeEditor) {
ThemeEditor(forTheme: $themeToEdit, isValid: $isValid)
}
正在行动
.onTapGesture {
/* themeInfo is of type struct ThemeContent: Codable, Hashable, Identifiable */
self.themeToEdit = game.themeInfo
self.showThemeEditor = true
}
我的目标是在视图之间传递值,从 Chooser 到 ThemeEditor。当用户按下一个图标时,我将保存我想要传递的对象,稍后使用 sheet
并传递包含 @State var
.
已成功分配给 @State var themeToEdit
,但是在 sheet
ThemeEditor
视图时它是 nil
我做错了什么?
struct Chooser: View {
@EnvironmentObject var store: Store
@State private var showThemeEditor = false
@State private var themeToEdit: ThemeContent?
@State private var editMode: EditMode = .inactive
@State private var isValid = false
var body: some View {
NavigationView {
List {
ForEach(self.store.games) { game in
NavigationLink(destination: gameView(game))
{
Image(systemName: "wrench.fill")
.imageScale(.large)
.opacity(editMode.isEditing ? 1 : 0)
.onTapGesture {
self.showThemeEditor = true
/* themeInfo is of type struct ThemeContent: Codable, Hashable, Identifiable */
self.themeToEdit = game.themeInfo
}
VStack (alignment: .leading) {
Text(self.store.name(for: something))
HStack{
/* some stuff */
Text(" of: ")
Text("Interesting info")
}
}
}
}
.sheet(isPresented: $showThemeEditor) {
if self.themeToEdit != nil { /* << themeToEdit is nil here - always */
ThemeEditor(forTheme: self.themeToEdit!, $isValid)
}
}
}
.environment(\.editMode, $editMode)
}
}
}
struct ThemeEditor: View {
@State private var newTheme: ThemeContent
@Binding var isValid: Bool
@State private var themeName = ""
init(forTheme theme: ThemeContent, isValid: Binding<Bool>) {
self._newTheme = State(wrappedValue: theme)
self._validThemeEdited = isValid
}
var body: some View {
....
}
}
struct ThemeContent: Codable, Hashable, Identifiable {
/* stores simple typed variables of information */
}
.sheet
内容视图是在创建时捕获的,所以如果你想检查里面的东西,你需要使用.sheet(item:)
变体,比如
.sheet(item: self.$themeToEdit) { item in
if item != nil {
ThemeEditor(forTheme: item!, $isValid)
}
}
注意:不清楚什么是 ThemeContent
,但可能需要使其符合其他协议。
使用Binding
。用这个改变你的 ThemeEditor 视图。
struct ThemeEditor: View {
@Binding private var newTheme: ThemeContent?
@Binding var isValid: Bool
@State private var themeName = ""
init(forTheme theme: Binding<ThemeContent?>, isValid: Binding<Bool>) {
self._newTheme = theme
self._isValid = isValid
}
var body: some View {
....
}
}
以及 sheet 代码
.sheet(isPresented: $showThemeEditor) {
ThemeEditor(forTheme: $themeToEdit, isValid: $isValid)
}
正在行动
.onTapGesture {
/* themeInfo is of type struct ThemeContent: Codable, Hashable, Identifiable */
self.themeToEdit = game.themeInfo
self.showThemeEditor = true
}