gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:19:18 26 4
gpt4 key购买 nike

iOS14 introducing errors with @State bindings 的后续问题
我正在显示来自多个选项的模式表,具体取决于按下的按钮。但是,现在在 iOS14 中,当工作表显示时,由于 selectedSpeaker/selectedMicrophone/selectedAmp 为 nil 导致出现 fatal error 。我正在尝试更改为 .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))
}
}
}
}
}

最佳答案

这是解决您的问题的另一种方法,它使用 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()
}
}
}

关于ios - 在 swiftUI 中显示来自多个选项的工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63938343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com