如何在不使用表格的情况下从 navigationBarItem 导航到新视图?
How do I navigate to a new View from a navigationBarItem without using Sheets?
我的应用程序的导航栏中有一个 "add" 按钮,我想显示另一个填充视图的视图,而不是像使用表格时默认情况下的视图那样模态显示。
代码如下:
.navigationBarItems(leading: EditButton(), trailing: Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
})
.sheet(isPresented: $showingAddScreen) {
NavigationView {
AddTaskView().environment(\.managedObjectContext, self.moc)
}
}
NavigationLink 将另一个视图推入堆栈。是否可以将 NavigationLink 与 navigationBarItems 一起使用,或者是否有更好的方式来全屏显示所需的视图?
你试过这样的事情吗?
.navigationBarItems(leading: NavigationLink(destination: YourAnotherView(), label: {
Text("Your Label or Icon")
}), trailing: NavigationLink(destination: YourAnotherView(), label: {
Text("Your label or Icon")
}))
在栏按钮项中使用 NavigationLink 没有问题。
使代码更简洁而不是像这样创建内联代码
.navigationBarItems(trailing: NavigationLink(destination: SecondView()) { Image(systemName: "plus") })
我会创建一个计算 属性
var addButton: some View {
NavigationLink(destination: SecondView()) { Image(systemName: "plus") })
}
// [...]
.navigationBarItems(trailing: addButton)
确保您只有 NavigationView
在第一个视图中
如果您想要更复杂的内容,请查看此 post How To Navigate Between Views In SwiftUI By Using An @ObservableObject
这是我在当前项目中使用的
我的应用程序的导航栏中有一个 "add" 按钮,我想显示另一个填充视图的视图,而不是像使用表格时默认情况下的视图那样模态显示。
代码如下:
.navigationBarItems(leading: EditButton(), trailing: Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
})
.sheet(isPresented: $showingAddScreen) {
NavigationView {
AddTaskView().environment(\.managedObjectContext, self.moc)
}
}
NavigationLink 将另一个视图推入堆栈。是否可以将 NavigationLink 与 navigationBarItems 一起使用,或者是否有更好的方式来全屏显示所需的视图?
你试过这样的事情吗?
.navigationBarItems(leading: NavigationLink(destination: YourAnotherView(), label: {
Text("Your Label or Icon")
}), trailing: NavigationLink(destination: YourAnotherView(), label: {
Text("Your label or Icon")
}))
在栏按钮项中使用 NavigationLink 没有问题。
使代码更简洁而不是像这样创建内联代码
.navigationBarItems(trailing: NavigationLink(destination: SecondView()) { Image(systemName: "plus") })
我会创建一个计算 属性
var addButton: some View {
NavigationLink(destination: SecondView()) { Image(systemName: "plus") })
}
// [...]
.navigationBarItems(trailing: addButton)
确保您只有 NavigationView
在第一个视图中
如果您想要更复杂的内容,请查看此 post How To Navigate Between Views In SwiftUI By Using An @ObservableObject
这是我在当前项目中使用的