0

我的目标是在视图之间传递值,从选择器到主题编辑器。当用户按下一个图标时,我正在保存我想要传递的对象,然后使用sheet和传递新创建的视图以及@State var.

分配已成功完成@State var themeToEdit,但它是nilThemeEditor视图中创建时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 */
}
4

2 回答 2

1

内容视图是在创建时捕获的.sheet,所以如果你想检查里面的东西,你需要使用.sheet(item:)变体,比如

.sheet(item: self.$themeToEdit) { item in
    if item != nil {
        ThemeEditor(forTheme: item!, $isValid)
    } 
}

注意:不清楚什么是ThemeContent,但可能需要使其符合其他协议。

于 2021-01-18T10:00:48.640 回答
1

使用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(isPresented: $showThemeEditor) {
    ThemeEditor(forTheme: $themeToEdit, isValid: $isValid)
}

在行动

.onTapGesture {
    /* themeInfo is of type struct ThemeContent: Codable, Hashable, Identifiable */
    self.themeToEdit = game.themeInfo
    self.showThemeEditor = true
    
}
于 2021-01-18T11:44:45.037 回答