SwiftUI 工具栏按钮不导航到另一个视图

问题描述 投票:0回答:1

我试图通过按下工具栏按钮导航到另一个视图,但它没有响应。我将按钮嵌入到 NavigationLink 中。我还能怎么做?

var body: some View {
    NavigationView {
        List {
            ForEach(items) { item in
                NavigationLink {
                    DisplayContactView()
                } label: {
                    Text(item.timestamp!, formatter: itemFormatter)
                }
            }
            .onDelete(perform: deleteItems)
        }
        .navigationTitle("Contacts")
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                EditButton()
                    .foregroundColor(.black)
            }
            ToolbarItem {
               NavigationLink(destination: AddContactView()) {
                Button(action: { })  {
                   Label("Add Contact", systemImage: "plus")
                   .foregroundColor(.black)
                 }
               }     
        }
        Text("Select a Contact")
    }
}
swiftui toolbar
1个回答
0
投票

这是你要找的吗:

struct ContentView: View {
    // A new state var on false by default
    @State var showAddView = false

    var body: some View {
        NavigationView {
            List {
                // ForEach + NavigationLink
            }
            .navigationTitle("Contacts")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Add", action: { showAddView = true })
                        // When the var is true the content is presented
                        // You can also use .toggle() instead of = true
                        .fullScreenCover(isPresented: $showAddView, content: { AddView() })
                }   
            }
        }
    }
}

这是另一个使用

.sheet
而不是
.fullScreenCover
的示例:https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-new-view-using-sheets

© www.soinside.com 2019 - 2024. All rights reserved.