联网请求完成后,我需要对某些视图进行刷新,但是刷新页面后,我的选项卡栏会自动切换到第一个选项卡视图,这是我的代码:
//tab view
struct PricesView: View {
@ObservedObject var netWorkManager = NetworkManager.shareInstance
var body: some View {
if netWorkManager.dataList.isEmpty {
LoadingView().onAppear(perform: netWorkManager.fetchPricesData)
} else {
Text("Prices").foregroundColor(.red)
}
}
}
...
//main view
struct ContentView: View {
var body: some View {
TabView {
HomeView().tabItem {
Image(systemName: "house.fill")
Text("home")
}
AlertsView().tabItem {
Image(systemName: "flag.fill")
Text("alerts")
}
LinksView().tabItem {
Image(systemName: "link.icloud")
Text("link")
}
PricesView().tabItem {
Image(systemName: "bitcoinsign.circle")
Text("prices")
}
}
}
}
我该如何避免呢?
使用选择和标签。
struct ContentView: View {
@State var selection = 0 // <- Here declare selection
var body: some View {
TabView(selection: $selection) { // <- Use selection here
HomeView().tabItem {
Image(systemName: "house.fill")
Text("home")
}.tag(0) // <- Add tag
AlertsView().tabItem {
Image(systemName: "flag.fill")
Text("alerts")
}.tag(1) // <- Add tag
LinksView().tabItem {
Image(systemName: "link.icloud")
Text("link")
}.tag(2) // <- Add tag
PricesView().tabItem {
Image(systemName: "bitcoinsign.circle")
Text("prices")
}.tag(3) // <- Add tag
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句