我目前正在處理 SwiftUI 下的 TabView 問題。
我在 EnvironmentObject 上選擇 TabView 的狀態(請參見下面的代碼),此時一切都很好,但是當我單擊一個選項卡時它會切換它,但是在它重新初始化 Tabview 之后,EnvironmentObject 將被重置為舊值.
我做錯了什么,在 iPad 上有效???
TabBarView.swift
struct TabBarView: View {
@EnvironmentObject var appData: AppDataModel
@State var activeSheet: ActiveSheet? = nil
init() {
UITabBar.appearance().isTranslucent = true
UITabBar.appearance().tintColor = UIColor(named: "TH-Accent")
}
var body: some View {
tabview
}
private var tabview: some View {
TabView(selection: $appData.currentTab) {
DashboardView()
.environmentObject(appData)
.tabItem {
Image(systemName: "rectangle.3.group.fill")
Text("Dashboard")
}
.tag(Tab.dashboard)
CalenderView()
.environmentObject(appData)
.tabItem {
Image(systemName: "calendar")
Text("Trainings")
}
.tag(Tab.calendar)
GalleryView()
.environmentObject(appData)
.tabItem {
Image(systemName: "photo.on.rectangle.angled")
Text("Galerie")
}
.tag(Tab.gallery)
TeamView()
.environmentObject(appData)
.tabItem {
Image(systemName: "person.3.fill")
Text("Team")
}
.tag(Tab.team)
SettingsView()
.environmentObject(appData)
.tabItem {
Image(systemName: "gearshape.fill")
Text("Einstellungen")
}
.tag(Tab.settings)
}
.accentColor(Color("TH-Accent"))
.onAppear {
print(appData.currentTab)
}
.sheet(item: $activeSheet) { item in
showActiveSheet(item: item)
}
}
}
MainView.swift
struct MainView: View {
@EnvironmentObject var appData: AppDataModel
var body: some View {
Group {
if UIDevice.isiPhone {
tabbar
} else {
sidebar
}
}
.environmentObject(appData)
.onOpenURL{ url in
if (appData.checkDeepLink(url: url)) {
print("from DEEP")
}
}
}
private var sidebar: some View {
SidebarView()
}
private var tabbar: some View {
TabBarView()
}
}
MainApp.swift(應用程式的起點)
@main
struct MainApp: App {
@StateObject var appData = AppDataModel()
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
init() {
var titleFont = UIFont.preferredFont(forTextStyle: .largeTitle) /// the default large title font
titleFont = UIFont(
descriptor:
titleFont.fontDescriptor
.withDesign(.rounded)? /// make rounded
.withSymbolicTraits(.traitBold) /// make bold
??
titleFont.fontDescriptor, /// return the normal title if customization failed
size: titleFont.pointSize
)
var smallTitleFont = UIFont.preferredFont(forTextStyle: .body) /// the default large title font
smallTitleFont = UIFont(
descriptor:
smallTitleFont.fontDescriptor
.withDesign(.rounded)? /// make rounded
.withSymbolicTraits(.traitBold) /// make bold
??
smallTitleFont.fontDescriptor, /// return the normal title if customization failed
size: smallTitleFont.pointSize
)
/// set the rounded font
UINavigationBar.appearance().largeTitleTextAttributes = [.font: titleFont]
UINavigationBar.appearance().titleTextAttributes = [.font : smallTitleFont]
}
var body: some Scene {
WindowGroup {
MainView()
.onAppear {
//Add and Init some values for the AppDataModel
}
.environmentObject(appData)
.alert(isPresented: $appData.noConnection, content: {
Alert(title: Text("Mobile Daten deaktiviert!"), message: Text("Versichere dich ob deine Mobilen Daten oder dein WLAN eingeschalten sind."), primaryButton: .cancel(Text("OK")), secondaryButton: .default(Text("Einstellungen"), action: {
if let url = URL(string: "prefs:root=MOBILE_DATA_SETTINGS_ID"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}))
})
}
}
}
uj5u.com熱心網友回復:
我通過創建 Tab 型別的單個 State 變數解決了我的問題,并添加了在 EnvironmentObject 中寫入的手勢。我認為這是 SwiftUI 中的一種解決方法和錯誤,因為我不再初始化 StateObject。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457717.html
標籤:IOS 迅速 苹果手机 迅捷 swiftui-tabview
