SwiftUI - 点击 TabItem 后 TabView 初始化
SwiftUI - TabView init after click on TabItem
我现在 运行 遇到 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)
}
}))
})
}
}
}
我通过创建一个类型为 Tab 的状态变量并添加了一个手势来写入 EnvironmentObject 来解决我的问题。我认为这是 SwiftUI 中的一个变通方法和一个错误,因为我没有再次初始化 StateObject。
我现在 运行 遇到 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)
}
}))
})
}
}
}
我通过创建一个类型为 Tab 的状态变量并添加了一个手势来写入 EnvironmentObject 来解决我的问题。我认为这是 SwiftUI 中的一个变通方法和一个错误,因为我没有再次初始化 StateObject。