簡體   English   中英

如何導航出 ActionSheet?

[英]How to navigate out of a ActionSheet?

如何導航出只能傳遞Text但不能傳遞NavigationLinkActionSheet

示例代碼:

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???")),
                ])
            }


        }
    }
}

你需要這樣的東西:

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和模態演示來實現)。 您還需要將 ShowHelp 作為@Binding以幫助視圖能夠重置它。

struct HelpView: View {
    
    @Binding var isShowing: Bool

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM