Skip to content

Commit 4bf2215

Browse files
committed
Add NavigationStack case study
1 parent 611ffd4 commit 4bf2215

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

ViewLifecycle.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
5D305D2A293266D000E42700 /* CaseStudyTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D305D29293266D000E42700 /* CaseStudyTabView.swift */; };
1818
5D60F905293FA5F400AE0726 /* CaseStudies.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D60F904293FA5F400AE0726 /* CaseStudies.swift */; };
1919
5D60F907293FA8D400AE0726 /* CaseStudyIfElse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D60F906293FA8D400AE0726 /* CaseStudyIfElse.swift */; };
20+
5D60F93F293FBBE800AE0726 /* CaseStudyNavigationStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D60F93E293FBBE800AE0726 /* CaseStudyNavigationStack.swift */; };
2021
5D6C3F6C2908541100A0C864 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D6C3F6B2908541100A0C864 /* App.swift */; };
2122
5D6C3F6E2908541100A0C864 /* RootView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D6C3F6D2908541100A0C864 /* RootView.swift */; };
2223
5D6C3F702908541200A0C864 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5D6C3F6F2908541200A0C864 /* Assets.xcassets */; };
@@ -34,6 +35,7 @@
3435
5D305D29293266D000E42700 /* CaseStudyTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaseStudyTabView.swift; sourceTree = "<group>"; };
3536
5D60F904293FA5F400AE0726 /* CaseStudies.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaseStudies.swift; sourceTree = "<group>"; };
3637
5D60F906293FA8D400AE0726 /* CaseStudyIfElse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaseStudyIfElse.swift; sourceTree = "<group>"; };
38+
5D60F93E293FBBE800AE0726 /* CaseStudyNavigationStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaseStudyNavigationStack.swift; sourceTree = "<group>"; };
3739
5D6C3F682908541100A0C864 /* ViewLifecycle.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ViewLifecycle.app; sourceTree = BUILT_PRODUCTS_DIR; };
3840
5D6C3F6B2908541100A0C864 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
3941
5D6C3F6D2908541100A0C864 /* RootView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootView.swift; sourceTree = "<group>"; };
@@ -77,6 +79,7 @@
7779
5D60F906293FA8D400AE0726 /* CaseStudyIfElse.swift */,
7880
5D0520B82934E6130080E4C0 /* CaseStudyLazyVGrid.swift */,
7981
5D305D232932547F00E42700 /* CaseStudyList.swift */,
82+
5D60F93E293FBBE800AE0726 /* CaseStudyNavigationStack.swift */,
8083
5D305D25293260BE00E42700 /* CaseStudyScrollViewVStackForEach.swift */,
8184
5D305D212932444500E42700 /* CaseStudyScrollView.swift */,
8285
5D0520B62934DC330080E4C0 /* CaseStudyStaticList.swift */,
@@ -174,6 +177,7 @@
174177
5D305D2A293266D000E42700 /* CaseStudyTabView.swift in Sources */,
175178
5D305D28293260EC00E42700 /* SampleModel.swift in Sources */,
176179
5D305D26293260BE00E42700 /* CaseStudyScrollViewVStackForEach.swift in Sources */,
180+
5D60F93F293FBBE800AE0726 /* CaseStudyNavigationStack.swift in Sources */,
177181
5D60F907293FA8D400AE0726 /* CaseStudyIfElse.swift in Sources */,
178182
5D305D222932444500E42700 /* CaseStudyScrollView.swift in Sources */,
179183
5D305D242932547F00E42700 /* CaseStudyList.swift in Sources */,

ViewLifecycle/CaseStudies.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,40 @@ extension CaseStudy {
1515
id: "ifElse",
1616
label: "if/else"
1717
)
18+
1819
static let scrollView: Self = .init(
1920
id: "scrollView",
2021
label: "ScrollView with static content"
2122
)
23+
2224
static let scrollViewVStackForEach: Self = .init(
2325
id: "scrollViewVStackForEach",
2426
label: "ScrollView > VStack > ForEach",
2527
description: "A VStack with dynamic content, embedded in a ScrollView"
2628
)
29+
2730
static let staticList: Self = .init(
2831
id: "staticList",
2932
label: "List with static content"
3033
)
34+
3135
static let list: Self = .init(
3236
id: "list",
3337
label: "List with dynamic content",
3438
description: "List recycles views during scrolling, so onAppear gets called often. But List preserves the State for all list items."
3539
)
40+
3641
static let lazyVGrid: Self = .init(
3742
id: "lazyVGrid",
3843
label: "LazyVGrid"
3944
)
45+
46+
static let navigationStack: Self = .init(
47+
id: "navigationStack",
48+
label: "NavigationStack",
49+
description: "A NavigationStack with infinite levels of drill-down."
50+
)
51+
4052
static let tabView: Self = .init(
4153
id: "tabView",
4254
label: "TabView",
@@ -70,15 +82,16 @@ let categories: [Category] = [
7082
),
7183
Category(
7284
id: "lazy",
73-
label: "Lazy Containers",
85+
label: "Lazy containers",
7486
elements: [
7587
.lazyVGrid,
7688
]
7789
),
7890
Category(
79-
id: "tabView",
80-
label: "TabView",
91+
id: "navigation",
92+
label: "Navigation containers",
8193
elements: [
94+
.navigationStack,
8295
.tabView,
8396
]
8497
),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import SwiftUI
2+
3+
struct CaseStudyNavigationStack: View {
4+
var body: some View {
5+
NavigationStack {
6+
Content(level: 1)
7+
}
8+
}
9+
10+
struct Content: View {
11+
var level: Int
12+
13+
var body: some View {
14+
List {
15+
Section {
16+
NavigationLink {
17+
Content(level: level + 1)
18+
} label: {
19+
LifecycleMonitor(label: "Level \(level)")
20+
}
21+
} footer: {
22+
if level == 1 {
23+
Text(explanation)
24+
.font(.callout)
25+
.frame(maxWidth: .infinity, alignment: .leading)
26+
}
27+
}
28+
}
29+
.listStyle(.plain)
30+
.navigationTitle(level == 1 ? "NavigationStack" : "Level \(level)")
31+
}
32+
33+
private var explanation: LocalizedStringKey {
34+
"Navigation views keep the state of content views on the navigation stack alive. `onAppear` and `onDisappear` get called as you navigate. Popping a view off the stack ends the view's lifetime, destroying its state."
35+
}
36+
}
37+
}
38+
39+
struct CaseStudyNavigationStack_Previews: PreviewProvider {
40+
static var previews: some View {
41+
CaseStudyNavigationStack()
42+
}
43+
}

ViewLifecycle/RootView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ struct MainContent: View {
6363
CaseStudyList()
6464
case .lazyVGrid:
6565
CaseStudyLazyVGrid()
66+
case .navigationStack:
67+
CaseStudyNavigationStack()
6668
case .tabView:
6769
CaseStudyTabView()
6870
default:

0 commit comments

Comments
 (0)