1

有谁知道我如何从 Swiftui 中的操作表转到另一个视图?

目前我将其用作操作表中的按钮:

.actionSheet(isPresented: $actionsheet) {
    ActionSheet(title: Text("Actions"), message: Text("Choose action"), buttons: [
        .default(
            NavigationLink(destination: adddetails()) {
                Text("Add details")
            }
        ),
        .default(Text("New")),
        .default(Text("Delete")),
        .cancel()
    ])                              
}

但它不会建立。即使 Xcode 没有给我错误。有谁知道我能做什么?

4

1 回答 1

1

您可以通过将参数与绑定一起使用以NavigationLink编程方式控制。isActive然后,在您的 中ActionSheet,您可以切换该绑定。

另一个关键是NavigationLink需要嵌入到您的原始视图层次结构中,而不是ActionSheet. 您可以使用语句有条件地显示它,if以便它仅在活动时才存在(因此除非按下导航按钮否则不可见):

struct ContentView: View {
    @State private var actionSheetOpen = false
    @State private var navigationLinkActive = false
    
    var body: some View {
        NavigationView {
            if navigationLinkActive {
                NavigationLink("", destination: Text("Detail"), isActive: $navigationLinkActive)
            }
            
            Button("Open action sheet") {
                actionSheetOpen.toggle()
            }
            .actionSheet(isPresented: $actionSheetOpen) {
                ActionSheet(title: Text("Actions"), message: Text("Choose action"), buttons: [
                    .default(Text("Navigation"), action: {
                        navigationLinkActive = true
                    }),
                    .default(Text("New")),
                    .default(Text("Delete")),
                    .cancel()
                ])
            }
            
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}
于 2021-03-15T13:57:10.357 回答