Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created August 21, 2023 01:02
Show Gist options
  • Save foxicode/1eac15299f3d1c5eaaf576f331e6e5d2 to your computer and use it in GitHub Desktop.
Save foxicode/1eac15299f3d1c5eaaf576f331e6e5d2 to your computer and use it in GitHub Desktop.
Navigation with data loading
import SwiftUI
struct ContentView: View {
@State var loadedContent: String?
var isActive: Binding<Bool> {
Binding(
get: { loadedContent != nil },
set: { _ in loadedContent = nil }
)
}
var body: some View {
NavigationView {
VStack {
Text("Original content view")
Text("Tap me to see the second screen")
.onTapGesture {
// Start some animation
// ...
// Simulate loading
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
// Stop animation
// ..
// Use loaded content
self.loadedContent = "Loaded content"
}
}
}
.padding()
.navigationTitle("Root view")
.background(
NavigationLink(
destination: InnerContentView(content: loadedContent ?? ""),
isActive: isActive
) {
EmptyView()
}
)
}
}
}
struct InnerContentView: View {
var content: String
var body: some View {
Text("Inner content view: \(content)")
.navigationTitle("Details view")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment