Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
import SwiftUI @Observable class AppState { var currentUser: String = "Alice" var isLoggedIn: Bool = true } struct RootView: View { @State private var appState = AppState() @State private var path = NavigationPath() var body: some View { NavigationStack(path: $path) { HomeView() .navigationDestination(for: Int.self) { id in DetailView(id: id) } .navigationDestination(for: String.self) { name in ProfileView(name: name) } } .environment(appState) } } struct HomeView: View { @Environment(AppState.self) private var appState @State private var showSheet = false var body: some View { List(1...5, id: \.self) { id in NavigationLink("Item \(id)", value: id) } .navigationTitle("Home") .toolbar { Button("Profile") { showSheet = true } } .sheet(isPresented: $showSheet) { ProfileView(name: appState.currentUser) .presentationDetents([.medium, .large]) } } } struct DetailView: View { let id: Int var body: some View { Text("Detail for item \(id)").navigationTitle("Item \(id)") } } struct ProfileView: View { let name: String var body: some View { Text("Profile: \(name)").font(.largeTitle) } }
Result
Open