如果父视图中已经有工作表,则无法在SwiftUI的子视图中显示工作表

时间:2020-07-30 13:53:52

标签: swiftui xcode12

是否可以在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! 第二张纸不能这样显示! 我必须在父视图中显示它!

1 个答案:

答案 0 :(得分:0)

解决方案是注意不要将.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")
            })
    }
}