我正在尝试实现一个菜单,目前已经有以下内容:
NavigationView
struct macOS_NavigationView: View {
@State private var selectedTab: HostingBarCategories = .Screen1
var body: some View {
NavigationView {
// SideBar Menu
List {
ForEach(1 ... 10, id: \.self) { index in
NavigationLink(destination:
Text("\(index)")
.frame(maxWidth: .infinity, maxHeight: .infinity)
) {
Text("Link \(index)")
}
}
}
.listStyle(SidebarListStyle())
// Primary View
Text("Select a menu...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
我卡住的部分是尝试在选项卡栏中实现我目前在iOS上使用的模型: HostingBarCategories
enum HostingBarCategories: Hashable {
case Screen1
case Screen2
case Screen3
case Screen4
case Screen5
}
那么我如何使用该模型,使得用户点击菜单后可以跳转到该屏幕?(该模型可以扩展,不一定非要使用特定的那一个)
编辑:让我添加当前的iOS TabBar,以便更加直观易懂,这只是对上面内容的参考,与问题无关:
struct iOS_TabBarView: View {
@State private var selectedTab: HostingBarCategories = .Screen1
var body: some View {
TabView(selection: $selectedTab) {
Text("1")
.tag(0)
.tabItem {
Image(systemName: "pencil.and.outline")
Text("1")
}
Text("2")
.tag(1)
.tabItem {
Image(systemName: "checkmark")
Text("2")
}
Text("3")
.tag(2)
.tabItem {
Image(systemName: "calendar.circle.fill")
Text("3")
}
Text("4")
.tag(3)
.tabItem {
Image(systemName: "flame")
Text("4")
}
Text("5")
.tag(3)
.tabItem {
Image(systemName: "slider.horizontal.3")
Text("5")
}
}
}
}
HostingBarCategories
如何与您的视图相关联?即HostingBarCategories
和你的视图之间应该有什么关系?” - pawello2222.tag(0)
,代表枚举类型:Screen1
。这有意义吗?还是让你更困惑了?整个重点是使用模型来构建 macOS 的菜单(侧边栏)。 - ElKePoN