3

是否可以在 SwiftUI 的子视图中显示工作表,其父视图中已经有工作表?

struct SheetTest: View {
    @State var showFirstSheet = false
  
    var body: some View {
        VStack {
            Text("FirstView")
                .onTapGesture(count: 1, perform: {
                    showFirstSheet = true
                })
            SomeView()
        }
        .sheet(isPresented: $showFirstSheet, content: {
            Text("firstsheet")
        })
            
    }
}
struct SomeView:View {
    @State var showSecondSheet = false
    var body: some View {
        Text("SecondView")
            .onTapGesture(count: 1, perform: {
                showSecondSheet = true
            })
            .sheet(isPresented: $showSecondSheet, content: {
                Text("secondsheet")
            })
    }
}

我目前正在使用 Xcode 12 beta 3!第二张纸不能以这种方式呈现!我必须在父视图中呈现它!

4

1 回答 1

3

解决方法是注意不要添加.sheet到同一个视图树。
我正在使用Xcode 12.1 (12A7403).

struct SheetTest: View {
    @State var showFirstSheet = false

    var body: some View {
        VStack {
            Text("FirstView")
                .onTapGesture(count: 1, perform: {
                    showFirstSheet = true
                })
                .sheet(isPresented: $showFirstSheet, content: {
                    Text("firstsheet")
                })
            SomeView()
        }
    }
}
struct SomeView:View {
    @State var showSecondSheet = false
    var body: some View {
        Text("SecondView")
            .onTapGesture(count: 1, perform: {
                showSecondSheet = true
            })
            .sheet(isPresented: $showSecondSheet, content: {
                Text("secondsheet")
            })
    }
}
于 2020-10-28T03:48:42.900 回答