我的ContentView具有两个不同的模式视图,因此我都使用sheet(isPresented:)了它们,但似乎只显示了最后一个。我该如何解决这个问题?还是无法在SwiftUI的视图上使用多个图纸?
sheet(isPresented:)
struct ContentView: View { @State private var firstIsPresented = false @State private var secondIsPresented = false var body: some View { NavigationView { VStack(spacing: 20) { Button("First modal view") { self.firstIsPresented.toggle() } Button ("Second modal view") { self.secondIsPresented.toggle() } } .navigationBarTitle(Text("Multiple modal view problem"), displayMode: .inline) .sheet(isPresented: $firstIsPresented) { Text("First modal view") } .sheet(isPresented: $secondIsPresented) { Text("Only the second modal view works!") } } } }
上面的代码编译时没有警告(Xcode 11.2.1)。
请尝试以下代码
enum ActiveSheet { case first, second } struct ContentView: View { @State private var showSheet = false @State private var activeSheet: ActiveSheet = .first var body: some View { NavigationView { VStack(spacing: 20) { Button("First modal view") { self.showSheet = true self.activeSheet = .first } Button ("Second modal view") { self.showSheet = true self.activeSheet = .second } } .navigationBarTitle(Text("Multiple modal view problem"), displayMode: .inline) .sheet(isPresented: $showSheet) { if self.activeSheet == .first { Text("First modal view") } else { Text("Only the second modal view works!") } } } } }