6

如何导航出ActionSheet我只能通过Text但不能通过的地方NavigationLink

示例代码:

struct DemoActionSheetNavi: View {
    @State private var showingSheet = false

    var body: some View {
        NavigationView {

            Text("Test")

            .actionSheet(isPresented: $showingSheet) {
                ActionSheet(
                    title: Text("What do you want to do?"),
                    message: Text("There's only one choice..."),
                    buttons: [
                        .default(Text("How to navigate from here to HelpView???")),
                ])
            }


        }
    }
}
4

1 回答 1

4

你需要这样的东西:

struct DemoActionSheetNavi: View {

    @State private var showingSheet = false
    @State private var showingHelp = false

    var body: some View {
        NavigationView {
            VStack {
                Text("Test")
                Button("Tap me") { self.showingSheet = true }
                NavigationLink(destination: HelpView(isShowing: $showingHelp),
                               isActive: $showingHelp) {
                    EmptyView()
                }
            }
            
        }
        .actionSheet(isPresented: $showingSheet) {
            ActionSheet(
                title: Text("What do you want to do?"),
                message: Text("There's only one choice..."),
                buttons: [.cancel(),
                          .default(Text("Go to help")) {
                            self.showingSheet = false
                            self.showingHelp = true
                    }])
        }
        
        
    }
}

您有另一种以编程方式触发 a 的状态NavigationLink(您也可以使用.sheet和模态演示来完成)。您还需要将showingHelp 作为@Binding帮助视图传递,以便能够重置它。

struct HelpView: View {
    
    @Binding var isShowing: Bool

    var body: some View {
        Text("Help view")
            .onDisappear() { self.isShowing = false }
        
    }
    
}
于 2019-12-23T11:32:34.237 回答