タップされたボタンに応じて複数のモーダルを表示できるビューを設定しようとしています。
を 1 つだけ追加するとsheet
、すべてが機能します。
.sheet(isPresented: $showingModal1) { ... }
しかし、別のシートを追加すると、最後のシートのみが機能します。
.sheet(isPresented: $showingModal1) { ... }
.sheet(isPresented: $showingModal2) { ... }
アップデート
これを動作させようとしましたが、 の型を宣言する方法がわかりませんmodal
。 というエラーが発生しますProtocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
。
struct ContentView: View {
@State var modal: View?
var body: some View {
VStack {
Button(action: {
self.modal = ModalContentView1()
}) {
Text("Show Modal 1")
}
Button(action: {
self.modal = ModalContentView2()
}) {
Text("Show Modal 2")
}
}.sheet(item: self.$modal, content: { modal in
return modal
})
}
}
struct ModalContentView1: View {
var body: some View {
Text("Modal 1")
}
}
struct ModalContentView2: View {
var body: some View {
Text("Modal 2")
}
}
ベストアンサー1
これは機能します:
.background(EmptyView().sheet(isPresented: $showingModal1) { ... }
.background(EmptyView().sheet(isPresented: $showingModal2) { ... }))
これらがどのようにネストされているかに注目してくださいbackgrounds
。2 つの背景が次々に続くわけではありません。
これを見つけてくれたDevAndArtistに感謝します。