繁体   English   中英

SwiftUI - 点击 TabItem 后 TabView 初始化

[英]SwiftUI - TabView init after click on TabItem

我目前正在处理 SwiftUI 下的 TabView 问题。

我在 EnvironmentObject 上选择了 TabView 的 state(见下面的代码),此时一切都很好,但是当我点击一个选项卡时它会切换它但是在它重新初始化 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)
        }
    }
}

主视图.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 的变量 State 解决了我的问题,并添加了一个手势来写入 EnvironmentObject。 我认为这是一种解决方法,也是 SwiftUI 中的一个错误,因为我没有再次初始化 StateObject。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM