Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Last active July 31, 2020 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Thomvis/8607f408999f069d66260e9900487c8b to your computer and use it in GitHub Desktop.
Save Thomvis/8607f408999f069d66260e9900487c8b to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var tab = 0
@State var selections: [Int:Int] = [:]
var body: some View {
ZStack {
TabView(selection: $tab) {
NavigationView {
PageView(n: 0, selections: $selections)
}.navigationViewStyle(StackNavigationViewStyle()).tabItem { Text("Pages") }.tag(0)
Text("Second").tabItem { Text("Second") }.tag(1)
}
VStack {
Text("Selections: \(selections.description)")
HStack {
Button(action: {
if let last = self.selections.keys.sorted().last {
self.selections.removeValue(forKey: last)
}
}) {
Text("Pop")
}
Button(action: {
if let last = self.selections.keys.sorted().last {
self.selections[last+1] = Int.random(in: 0..<10)
} else {
self.selections[0] = 0
}
}) {
Text("Push")
}
}
}
.padding(8)
.background(Color(UIColor.systemGray3).cornerRadius(8))
.frame(maxHeight: .infinity, alignment: .bottom)
.padding(.bottom, 100)
}
}
}
struct PageView: View {
let n: Int
@Binding var selections: [Int:Int]
var body: some View {
List {
ForEach(0..<10) { i in
NavigationLink(destination: PageView(n: n+1, selections: $selections), isActive: isActive(i)) {
Text("\(i)")
}
}
}
}
func isActive(_ i: Int) -> Binding<Bool> {
Binding(get: {
self.selections[self.n] == i
}, set: {
self.selections[self.n] = $0 ? i : nil
})
}
}
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