如何导航到ActionSheet?

时间:2019-12-23 11:18:12

标签: swiftui swiftui-navigationlink swiftui-actionsheet

如何导航到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???")),
                ])
            }


        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要这样的东西:

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
                    }])
        }


    }
}

您还有另一种状态,可以通过编程方式触发NavigationLink(您也可以使用.sheet和模式表示来执行此操作)。您还需要将showingHelp作为@Binding传递,以帮助视图将其重置。

struct HelpView: View {

    @Binding var isShowing: Bool

    var body: some View {
        Text("Help view")
            .onDisappear() { self.isShowing = false }

    }

}