Skip to content

Instantly share code, notes, and snippets.

@filimo
Last active November 25, 2022 14:26
Show Gist options
  • Save filimo/a3b5a92c6a0c7b670b97604cd70c521b to your computer and use it in GitHub Desktop.
Save filimo/a3b5a92c6a0c7b670b97604cd70c521b to your computer and use it in GitHub Desktop.
import SwiftUI
enum NavTestRoute: Hashable {
case child(Int)
}
struct NavTestChildView: View {
let num: Int
init(num: Int) {
print("[init][NavTestChildView]")
self.num = num
}
var body: some View {
Text("NavTestChildView \(num)")
}
}
struct NavTestMainView2: View {
init() {
print("[init][NavTestMainView2]")
}
var body: some View {
VStack {
NavigationLink {
NavTestMainView2()
} label: {
Text("Reenter NavTestMainView")
}
ForEach(1 ..< 10, id: \.self) { num in
NavigationLink(value: NavTestRoute.child(num)) {
Text("Open child \(num)")
}
}
}
.navigationDestination(for: NavTestRoute.self) { route in
switch route {
case let .child(num):
NavTestChildView(num: num)
}
}
}
}
struct NavTestMainView: View {
init() {
print("[init][NavTestMainView]")
}
var body: some View {
NavigationStack {
NavTestMainView2()
}
}
}
enum NavTestRoute: Hashable {
case child(Int)
}
struct NavTestChildView: View {
let num: Int
init(num: Int) {
print("[init][NavTestChildView]")
self.num = num
}
var body: some View {
Text("NavTestChildView \(num)")
}
}
struct NavTestMainView2: View {
@Binding var items: [Int]
init(items: Binding<[Int]>) {
print("[init][NavTestMainView2]")
self._items = items
}
var body: some View {
VStack {
NavigationLink {
NavTestMainView2(items: $items)
} label: {
Text("Reenter NavTestMainView")
}
ForEach(1 ..< 10, id: \.self) { num in
NavigationLink(value: NavTestRoute.child(num)) {
Text("Open child \(num)")
}
}
}
.navigationDestination(for: NavTestRoute.self) { route in
switch route {
case let .child(num):
NavTestChildView(num: num)
}
}
}
}
struct NavTestMainView: View {
@State var items: [Int] = [1, 2, 3, 4]
init() {
print("[init][NavTestMainView]")
}
var body: some View {
NavigationStack {
NavTestMainView2(items: $items)
}
}
}
import SwiftUI
enum NavTestRoute: Hashable {
case child(Int)
}
struct NavTestChildView: View {
let num: Int
var body: some View {
Text("NavTestChildView \(num)")
}
}
struct NavTestMainView_Control: View {
let num: Int
let isDetailMode: Bool
@Binding var items: [Int]
var body: some View {
VStack {
if isDetailMode {
Text("Long mode \(num)")
ForEach(items, id: \.self) { item in
NavigationLink(value: NavTestRoute.child(item)) {
Text("Go to \(item)")
}
}
} else {
Text("Short mode \(num)")
}
}
}
}
struct NavTestMainView2: View {
@State var items: [Int] = []
var body: some View {
ForEach(1 ..< 5) { num in
NavigationLink {
NavTestMainView_Control(num: num, isDetailMode: true, items: $items)
} label: {
NavTestMainView_Control(num: num, isDetailMode: false, items: $items)
}
.task {
items = [1, 2, 4]
}
.navigationDestination(for: NavTestRoute.self) { route in
switch route {
case let .child(num):
NavTestChildView(num: num)
}
}
}
}
}
struct NavTestMainView: View {
init() {
print("[init][NavTestMainView]")
}
var body: some View {
NavigationStack {
NavTestMainView2()
}
}
}
@filimo
Copy link
Author

filimo commented Nov 25, 2022

поменял на extension Binding: Equatable where Value: Hashable { иначе ломается .searchable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment