[英]SwiftUI Navigation Bar Item with different destinations
I have a Navigation View with an Add button(NavigationBarItem) and want to set a different destination of the Navigation Bar item in each tab.我有一个带有添加按钮 (NavigationBarItem) 的导航视图,我想在每个选项卡中设置不同的导航栏项目目的地。
enum Tab: String {
case income = "Income"
case expenses = "Expenses"
case budgets = "Budget"
case investment = "Investment"
case assets = "Asset"
}
I have already tried to give the struct the view as raw value but this is not possible.我已经尝试将视图作为原始值提供给结构,但这是不可能的。 How can I do this?
我怎样才能做到这一点?
var body: some View {
NavigationView {
TabView(selection: $selection) {
Text("Place Holder Income")
.tabItem {
Label("Income", systemImage: "star")
}
.tag(Tab.income)
.navigationBarHidden(false)
ExpensesView()
.tabItem {
Label("Expenses", systemImage: "star")
}
.tag(Tab.expenses)
.navigationBarHidden(false)
Text("Place Holder Budgets")
.tabItem {
Label("Budgets", systemImage: "star")
}
.tag(Tab.budgets)
.navigationBarHidden(false)
Text("Place Holder Investment")
.tabItem {
Label("Investment", systemImage: "star")
}
.tag(Tab.investment)
.navigationBarHidden(false)
Text("Place Holder Assets")
.tabItem {
Label("Assets", systemImage: "star")
}
.tag(Tab.assets)
.navigationBarHidden(false)
}
.navigationTitle(selection.rawValue)
.navigationBarItems(
trailing:
NavigationLink("Add", destination: AddExpensesView())
)
}
}
You could add a function (in your view) that routes to the right destination (depending on the value of the "selection" property).您可以添加一个 function(在您看来)路由到正确的目的地(取决于“选择”属性的值)。
@ViewBuilder func addView() -> some View {
switch selection {
case .income: Text("Income destination")
case .expenses: Text("Expenses destination")
default: Rectangle()
}
}
Your NavigationLink
calls this view builder:您的
NavigationLink
调用此视图生成器:
NavigationLink("Add", destination: addView())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.