在 swiftUI 中显示来自多个选项的工作表

Posted

技术标签:

【中文标题】在 swiftUI 中显示来自多个选项的工作表【英文标题】:Displaying a Sheet from multiple options in swiftUI 【发布时间】:2020-09-17 12:46:09 【问题描述】:

追问ios14 introducing errors with @State bindings

我从几个选项中显示了一个模式表,具体取决于按下的按钮。但是,现在在 iOS14 中,当工作表显示时,由于 selectedSpeaker/selectedMicrophone/selectedAmp 为 nil 导致出现致命错误。我正在尝试更改为.sheet(item:, content:),但我看不到如何实现枚举,然后传入适当的选定对象。这就是我之前所做的:

enum ActiveSheet 
    case speakerDetails, micDetails, ampDetails, settings

struct FavoritesView: View 
    @State private var selectedSpeaker: Speaker?
    @State private var selectedMicrophone: Microphone?
    @State private var selectedAmp: Amplifier?

    @State private var showingSheet = false
    @State private var activeSheet: ActiveSheet = .settings  

    var body: some View 
    
    List 
        Button(action: 
            self.activeSheet = .settings
            self.showingSheet = true
            , label:  Text("Settings"))

        Button(action: 
            self.activeSheet = .micDetails
            self.selectedMicrophone = microphones[0]
            self.showingSheet = true
            , label:  Text("Mic 1"))

        Button(action: 
            self.activeSheet = .micDetails
            self.selectedMicrophone = microphones[1]
            self.showingSheet = true
            , label:  Text("Mic 2"))

        Button(action: 
            self.activeSheet = .speakerDetails
            self.showingSheet = true
            self.selectedSpeaker = speakers[0]
            , label:  Text("Speaker 1"))

        Button(action: 
            self.activeSheet = .speakerDetails
            self.showingSheet = true
            self.selectedSpeaker = speakers[1]
            , label:  Text("Speaker 2"))

    //and so on for activeSheet = .ampDetails in the same way.
        
    
      .sheet(isPresented: self.$showingSheet) 
                if self.activeSheet == .speakerDetails 
                    SpeakerDetailView(speaker: self.selectedSpeaker!)
                
                else if self.activeSheet == .micDetails 
                MicDetailView(microphone: self.selectedMicrophone!)
                
                else if self.activeSheet == .ampDetails 
                AmpDetailView(amp: self.selectedAmp!)
                 else if self.activeSheet == .settings 
                    SettingsView(showSheet: self.$showingSheet))
                
            
        
    

【问题讨论】:

这可能会帮助你***.com/questions/58737767/… 【参考方案1】:

这是解决您的问题的另一种方法,它使用 sheet(item:content:)

struct ContentView: View 
    @State private var selectedSpeaker: Speaker?
    @State private var selectedMicrophone: Microphone?
    @State private var selectedAmp: Amplifier?
    @State private var showSettingsSheet = false

    var body: some View 
        List 
            settingsSection
            microphonesSection
            // more sections
        
    
    
    var settingsSection: some View 
        Button(action: 
            self.showSettingsSheet = true
        ) 
            Text("Settings")
        
        .sheet(isPresented: self.$showSettingsSheet) 
            SettingsView()
        
    
    
    @ViewBuilder
    var microphonesSection: some View 
        Button(action: 
            self.selectedMicrophone = microphones[0]
        ) 
            Text("Mic 1")
        
        Button(action: 
            self.selectedMicrophone = microphones[1]
        ) 
            Text("Mic 2")
        
        .sheet(item: self.$selectedMicrophone) 
            MicDetailView(microphone: $0)
        
    

这样你也不需要enum ActiveSheet


您始终可以使用@Environment(\.presentationMode) 关闭工作表,无需将变量传递给工作表(如SettingsView(showSheet: self.$showingSheet)):

struct SettingsView: View 
    @Environment(\.presentationMode) private var presentationMode

    var body: some View 
        Text("SettingsView")
            .onTapGesture 
                presentationMode.wrappedValue.dismiss()
            
    

【讨论】:

以上是关于在 swiftUI 中显示来自多个选项的工作表的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 工作表显示数据错误的工作表

使用 SwiftUI 时尝试在视图中显示工作表时应用程序崩溃

SwipeActions 按钮和 ToolBar 按钮触发的多个工作表(项目:)在 SwiftUI 中首次获取 nil 对象

SwiftUI:工作表第一次无法显示正确的值

表单/表格中的 SwiftUI 多个 NavigationLink - 条目保持突出显示

基于显示 2 选择的工作表视图的 SwiftUI 导航决策