首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SwiftUI:支持多通道

SwiftUI:支持多通道
EN

Stack Overflow用户
提问于 2019-07-19 07:29:59
回答 8查看 9.2K关注 0票数 36

我正在尝试设置一个视图,它可以根据点击的按钮显示多个模态。

当我只添加一个sheet时,一切都正常:

代码语言:javascript
运行
复制
.sheet(isPresented: $showingModal1) { ... }

但是当我添加另一个工作表时,只有最后一个工作表有效。

代码语言:javascript
运行
复制
.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的错误。

代码语言:javascript
运行
复制
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")
    }
}
EN

回答 8

Stack Overflow用户

发布于 2019-09-10 22:37:06

这是可行的:

代码语言:javascript
运行
复制
.background(EmptyView().sheet(isPresented: $showingModal1) { ... }
   .background(EmptyView().sheet(isPresented: $showingModal2) { ... }))

请注意这些是如何嵌套的backgrounds。不是一个接一个的两个背景。

感谢DevAndArtist找到了这一点。

票数 40
EN

Stack Overflow用户

发布于 2019-07-19 13:04:44

也许我没有抓住要点,但您可以通过对.sheet()的一次调用或多次调用来实现。

Multiple .sheet() approach:

代码语言:javascript
运行
复制
import SwiftUI

struct MultipleSheets: View {
    @State private var sheet1 = false
    @State private var sheet2 = false
    @State private var sheet3 = false

    var body: some View {
        VStack {

            Button(action: {
                self.sheet1 = true
            }, label: { Text("Show Modal #1") })
            .sheet(isPresented: $sheet1, content: { Sheet1() })

            Button(action: {
                self.sheet2 = true
            }, label: { Text("Show Modal #2") })
            .sheet(isPresented: $sheet2, content: { Sheet2() })

            Button(action: {
                self.sheet3 = true
            }, label: { Text("Show Modal #3") })
            .sheet(isPresented: $sheet3, content: { Sheet3() })

        }
    }
}

struct Sheet1: View {
    var body: some View {
        Text("This is Sheet #1")
    }
}

struct Sheet2: View {
    var body: some View {
        Text("This is Sheet #2")
    }
}

struct Sheet3: View {
    var body: some View {
        Text("This is Sheet #3")
    }
}

Single .sheet()方法:

代码语言:javascript
运行
复制
struct MultipleSheets: View {
    @State private var showModal = false
    @State private var modalSelection = 1

    var body: some View {
        VStack {

            Button(action: {
                self.modalSelection = 1
                self.showModal = true
            }, label: { Text("Show Modal #1") })

            Button(action: {
                self.modalSelection = 2
                self.showModal = true
            }, label: { Text("Show Modal #2") })

            Button(action: {
                self.modalSelection = 3
                self.showModal = true
            }, label: { Text("Show Modal #3") })

        }
        .sheet(isPresented: $showModal, content: {
            if self.modalSelection == 1 {
                Sheet1()
            }

            if self.modalSelection == 2 {
                Sheet2()
            }

            if self.modalSelection == 3 {
                Sheet3()
            }
        })

    }
}

struct Sheet1: View {
    var body: some View {
        Text("This is Sheet #1")
    }
}

struct Sheet2: View {
    var body: some View {
        Text("This is Sheet #2")
    }
}

struct Sheet3: View {
    var body: some View {
        Text("This is Sheet #3")
    }
}
票数 30
EN

Stack Overflow用户

发布于 2020-02-18 16:32:17

我不确定这是否总是可能的,但是在Xcode11.3.1中,对于这种用例(https://developer.apple.com/documentation/swiftui/view/3352792-sheet),有一个重载的.sheet()。您可以使用可识别的项而不是布尔值来调用它:

代码语言:javascript
运行
复制
struct ModalA: View {

    var body: some View {
        Text("Hello, World! (A)")
    }

}

struct ModalB: View {

    var body: some View {
        Text("Hello, World! (B)")
    }

}

struct MyContentView: View {

    enum Sheet: Hashable, Identifiable {

        case a
        case b

        var id: Int {
            return self.hashValue
        }

    }

    @State var activeSheet: Sheet? = nil

    var body: some View {
        VStack(spacing: 42) {
            Button(action: {
                self.activeSheet = .a
            }) {
                Text("Hello, World! (A)")
            }
            Button(action: {
                self.activeSheet = .b
            }) {
                Text("Hello, World! (B)")
            }
        }
            .sheet(item: $activeSheet) { item in
                if item == .a {
                    ModalA()
                } else if item == .b {
                    ModalB()
                }
            }
    }

}
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57103800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档