我的目标是在视图之间传递值,从选择器到主题编辑器。当用户按下一个图标时,我正在保存我想要传递的对象,然后使用sheet
和传递新创建的视图以及@State var
.
分配已成功完成@State var themeToEdit
,但它是nil
在ThemeEditor
视图中创建时sheet
我究竟做错了什么?
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 */
}