Sunday, March 17, 2024
HomeiOS Developmentios - How can I modify root view in SwiftUI (with out...

ios – How can I modify root view in SwiftUI (with out UIHostingController)?


Discovered an answer with none further stuff(e.g. RootView struct).
I’ve right here instance challenge with 3 screens, all of them can swap rootView from App struct through NavigationManager.

Most important App struct:

@predominant
struct SwiftUI_TestApp: App {
    
    @StateObject var navigationManager: NavigationManager
    
    init() {
        _navigationManager = StateObject(wrappedValue: NavigationManager(rootScreen: .predominant))
    }
    
    var physique: some Scene {
        WindowGroup {
            swap navigationManager.rootScreen {
            case .content material:
                ContentView()
                    .environmentObject(navigationManager)
            case .predominant:
                MainView()
                    .environmentObject(navigationManager)
            case .settings:
                SettingsView()
                    .environmentObject(navigationManager)
            }
        }
    }
}

NavigationManager class:

remaining class NavigationManager: ObservableObject {
    
    enum Display: Hashable {
        case predominant
        case content material
        case settings
    }
    
    @Revealed var rootScreen: Display
    
    init(rootScreen: Display) {
        self.rootScreen = rootScreen
    }
}

ContentView struct:

struct ContentView: View {
    
    @EnvironmentObject var navigationManager: NavigationManager
    
    var physique: some View {
        VStack {
            Textual content("Content material")
            
            Button {
                navigationManager.rootScreen = .predominant
            } label: {
                Textual content("Present Most important")
            }
            
            Button {
                navigationManager.rootScreen = .settings
            } label: {
                Textual content("Present Settings")
            }
        }
    }
}

MainView struct:

struct MainView: View {
    
    @EnvironmentObject var navigationManager: NavigationManager
    
    var physique: some View {
        VStack {
            Textual content("Most important")
            
            Button {
                navigationManager.rootScreen = .content material
            } label: {
                Textual content("Present Content material")
            }
            
            Button {
                navigationManager.rootScreen = .settings
            } label: {
                Textual content("Present Settings")
            }
        }
    }
}

SettingsView struct:

struct SettingsView: View {
    
    @EnvironmentObject var navigationManager: NavigationManager
    
    var physique: some View {
        VStack {
            Textual content("Settings")
            
            Button {
                navigationManager.rootScreen = .content material
            } label: {
                Textual content("Present Content material")
            }
            
            Button {
                navigationManager.rootScreen = .predominant
            } label: {
                Textual content("Present Most important")
            }
        }
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments