How to use NavigationStack in SwiftUI | Bootcamp #62

Swiftful Thinking
6 Jun 202323:59

Summary

TLDRNick在Swift UI训练营的视频中介绍了Swift UI的最新更新,特别是iOS 16中引入的导航栈(Navigation Stack)。他解释了导航栈如何取代了旧的导航视图(Navigation View),并强调了其懒加载(lazy loading)的特性,这使得应用程序在需要时才加载屏幕,从而提高了性能和可扩展性。此外,Nick还展示了如何使用导航目的地(Navigation Destinations)来处理不同类型的数据,并演示了如何通过路径绑定(path binding)来以编程方式操作导航栈,包括一次性推送多个屏幕到导航栈。他鼓励开发者尽快从导航视图迁移到导航栈,因为这是Swift UI和iOS 16中一个重要的进步。

Takeaways

  • 📚 Swift UI自发布以来经历了多次更新,现在是时候回顾并增加一些新特性的介绍了。
  • 🚀 iOS 16带来了许多新特性,其中导航栈(Navigation Stack)是非常酷的一个,值得分享。
  • 🔄 导航栈相比导航视图(Navigation View)提供了更多功能,并且支持懒加载,这在生产级别的可扩展应用中非常重要。
  • 🛠️ 在iOS 16及以上版本中,推荐使用导航栈而不是导航视图,因为导航视图已被弃用。
  • 📈 导航栈允许我们更灵活地管理导航路径,可以一次性推送多个屏幕到导航路径中。
  • 🔗 导航链接(Navigation Link)在导航栈中仍然可以使用,并且可以通过绑定(Binding)来动态控制导航路径。
  • 🧩 导航栈支持为不同类型的数据设置多个导航目的地,这对于构建可扩展的应用程序非常有帮助。
  • 📝 使用导航栈时,每个目的地视图需要符合哈希协议(Hashable),以便导航栈可以正确处理。
  • ➡️ 导航栈的路径(Path)可以绑定到一个状态变量,允许我们以编程方式修改导航路径,实现更高级的导航控制。
  • 🔄 导航栈的懒加载特性意味着只有在需要时才会加载屏幕,这有助于提高应用性能并减少不必要的资源消耗。
  • 📱 对于初学者和中级开发者来说,导航栈提供了更简单、更高效的方式来处理导航,而无需自己实现复杂的懒加载逻辑。
  • ⏪ 如果需要从导航栈返回,它将按照进入的顺序返回,而不是直接回到最初的屏幕,这对于复杂的应用流程非常有用。

Q & A

  • Swift UI自发布以来有哪些重大更新?

    -自Swift UI发布以来,已经有很多更新,特别是在iOS 16中,引入了一些非常酷的新特性。

  • 为什么推荐使用Navigation Stack而不是Navigation View?

    -Navigation Stack提供了比Navigation View更多的功能,例如支持iOS 16或以上版本时,推荐使用Navigation Stack,因为它提供了懒加载,可以更高效地加载屏幕。

  • 什么是懒加载(lazy loading),为什么它对应用程序很重要?

    -懒加载是一种按需加载资源的技术,只有当需要时才加载屏幕或数据。这对于生产级别的可扩展应用程序非常重要,因为它可以减少不必要的资源消耗,提高应用性能。

  • 在Swift UI中,Navigation Stack如何实现懒加载?

    -Navigation Stack通过使用Navigation Destination来实现懒加载,只有当用户实际导航到特定屏幕时,才会初始化和加载该屏幕。

  • 如果我之前使用了Navigation View,我需要立即切换到Navigation Stack吗?

    -虽然Navigation View已被弃用,但仍然可以在短期内使用。不过,推荐开发者尽快切换到Navigation Stack,以利用其更高效和更现代的功能。

  • Navigation Stack是否支持多数据类型的导航目的地?

    -是的,Navigation Stack支持多数据类型的导航目的地。开发者可以为不同类型的数据定义不同的导航目的地,这有助于构建更动态和可扩展的应用程序。

  • Navigation Stack中的path binding是如何工作的?

    -path binding允许开发者将Navigation Stack绑定到一个可以动态修改的路径上。这意味着可以通过编程方式向路径中添加或删除目的地,从而实现复杂的导航流程。

  • 如何在Navigation Stack中实现一次性推送多个屏幕?

    -通过使用path binding,可以一次性向Navigation Stack的路径中添加多个元素,从而实现一次性推送多个屏幕。这在iOS 15中是不可能的,但Navigation Stack提供了这种能力。

  • 使用Navigation Stack时,如何初始化视图以预设的绑定开始?

    -可以通过在视图的状态中设置一个初始的path binding来实现。这样,当视图加载时,Navigation Stack将从预设的路径开始导航。

  • 如果我已经在我的应用程序中使用了Navigation View,我该如何迁移到Navigation Stack?

    -可以逐步迁移。首先,将现有的Navigation View替换为Navigation Stack,但保持现有的Navigation Links不变。然后,逐渐将Navigation Links转换为使用Navigation Destinations。

  • Navigation Stack是否只能在iOS 16或更高版本上使用?

    -Navigation Stack是在iOS 16中引入的,因此为了使用Navigation Stack,你的应用程序需要支持iOS 16或更高版本。

Outlines

00:00

📚 Swift UI 导航栈介绍

Nick在Swift UI训练营中介绍了Swift UI的新特性,特别是iOS 16中引入的导航栈。他提到虽然导航视图仍然可以使用,但推荐使用新的导航栈,因为它提供了更多的功能和改进,包括懒加载和更好的性能。Nick还解释了导航栈的使用方法,并强调了它与导航视图相比的优势。

05:02

🚀 导航栈的懒加载特性

Nick通过一个示例展示了导航栈的懒加载特性,说明了在旧版导航视图中,即使没有点击,所有屏幕都会被预先加载,这可能会影响应用性能。而导航栈仅在用户实际访问时加载屏幕,这样可以显著提高应用程序的响应速度和性能,尤其是在大型应用中。

10:02

🔄 导航栈的使用和目的地

Nick演示了如何使用导航栈,并解释了如何通过导航目的地进行屏幕间的跳转。他展示了如何使用`NavigationDestination`修饰符来定义不同类型的数据目的地,并且可以为不同的数据类型设置多个导航目的地,从而使得导航更加灵活和可扩展。

15:02

🔗 绑定导航路径

Nick介绍了导航栈中的`path`绑定,它允许开发者以编程方式修改导航路径。通过绑定到一个数组,可以在应用中动态地添加或改变导航目的地。此外,他还提到了如何通过预设的路径绑定来初始化视图,从而在应用启动时直接导航到特定的屏幕。

20:03

➡️ 导航栈的高级用法

最后,Nick讨论了导航栈的高级用法,包括如何使用`superSeque`来一次性推送多个屏幕到导航栈。他指出,虽然这种用法在基础应用中不常见,但对于大型和可扩展的应用来说,这是一个非常有用的功能。他还提到了如何自定义和覆盖导航栈路径,尽管这更高级且有些hacky,但他建议大多数应用可能不需要这么做。

Mindmap

Keywords

💡Swift UI

Swift UI是一个由苹果公司开发的用于构建用户界面的框架,它允许开发者以声明式的方式创建动态和响应式的iOS、iPadOS、macOS、tvOS以及watchOS应用程序界面。在视频中,Swift UI是讨论的核心,因为视频的主题是关于Swift UI的更新和新特性。

💡iOS 16

iOS 16是苹果公司为其移动设备操作系统发布的一个主要版本。视频中提到,iOS 16带来了很多Swift UI的新特性,这些特性是视频讨论的重点之一。

💡导航栈(Navigation Stack)

导航栈是Swift UI中用于管理视图导航层次结构的新方式,它允许更复杂的导航流程,如同时推送多个视图。视频中强调了导航栈比旧的导航视图(NavigationView)更高效,支持懒加载,并提供了更强大的功能。

💡懒加载(Lazy Loading)

懒加载是一种编程技术,意味着直到真正需要某个资源或数据时才会加载它。在Swift UI中,导航栈支持懒加载视图,这可以提高应用程序的性能,因为它避免了提前加载不必要的屏幕。视频中通过一个示例展示了懒加载如何在导航栈中工作。

💡导航链接(Navigation Link)

导航链接是Swift UI中的一个组件,用于在应用程序的不同视图之间进行导航。在视频中,导航链接被用来演示如何在导航视图和导航栈中推动到新的视图。

💡导航视图(NavigationView)

导航视图是Swift UI中的一个组件,用于创建带有导航栏的视图层次结构。然而,视频中提到,尽管导航视图仍然有效,但在iOS 16及以上版本中,推荐使用导航栈,因为导航视图已被弃用。

💡数据类型(Data Type)

数据类型定义了可以在变量或参数中存储的值的种类。在视频中,数据类型对于理解导航目的地(Navigation Destination)如何工作至关重要,因为它们需要是哈希协议的遵守者,如整数或字符串。

💡哈希协议(Hashable Protocol)

哈希协议是Swift中的一个协议,它允许类型被用作字典的键或集合中的元素。在视频中,当创建导航目的地时,需要指定一个符合哈希协议的数据类型,以便Swift UI可以跟踪和比较不同的目的地。

💡视图结构(View Structure)

视图结构指的是在Swift UI中如何组织和嵌套视图组件来创建用户界面。视频中通过创建不同的视图结构来演示如何使用导航栈和导航链接,以及如何通过视图结构来控制应用程序的导航流程。

💡初始化(Initialization)

初始化是编程中的一个概念,指的是对象或结构体在创建时设置其初始状态的过程。视频中通过打印初始化信息来演示视图何时被创建,以及懒加载如何在导航栈中避免不必要的初始化。

💡导航路径(Navigation Path)

导航路径是Swift UI中用于表示导航栈当前状态的一个绑定。在视频中,通过绑定导航路径到一个状态变量,可以控制和监视导航流程,甚至实现通过编程方式修改导航堆栈,如添加多个目的地。

Highlights

Swift UI自发布以来经历了许多更新,现在是时候回顾并增加一些新特性的介绍视频。

iOS 16带来了许多酷炫的新特性,特别是导航栈(Navigation Stack)的改进。

导航栈(Navigation Stack)比导航视图(Navigation View)提供了更多的功能和优势。

导航栈允许懒加载(lazy loading),仅在需要时加载屏幕,这对于生产级别的可扩展应用至关重要。

导航栈支持多屏幕同时推送,这是iOS 15所不支持的新功能。

导航栈可以通过路径绑定(path binding)实现动态路由,这在构建大型应用时非常有用。

即使在iOS 16中,导航视图仍然可以工作,但是推荐使用导航栈以获得更好的性能和新特性。

导航栈允许开发者以类型安全的方式定义导航目的地,支持整数和字符串等多种数据类型。

使用导航栈可以避免之前在自定义懒加载导航时遇到的复杂性。

导航栈的路径可以被初始化为预设的绑定,从而在应用启动时直接导航到特定的屏幕。

导航栈的引入是Swift UI在iOS 16中最重要的进步之一,它极大地提高了应用的可扩展性和性能。

通过使用导航栈,开发者可以更简单地实现之前需要复杂自定义代码才能完成的导航功能。

导航栈提供了一种新的方式来处理应用中的导航,使得导航更加灵活和强大。

导航栈的懒加载特性意味着只有在用户实际访问某个屏幕时,该屏幕才会被加载。

导航栈允许开发者为不同的数据类型定义多个导航目的地,从而实现更精细的导航控制。

导航栈的路径绑定可以动态地向导航堆栈中添加新的目的地,支持更复杂的导航场景。

即使对于初学者和中级开发者,导航栈也提供了足够的功能来满足大多数应用的导航需求。

对于需要同时推送多个屏幕的复杂应用场景,导航栈提供了超级跳转(Super Segue)的功能。

Transcripts

play00:00

all right welcome back everybody my name

play00:02

is Nick this is swiffel thinking of

play00:04

course and we're back here in the Swift

play00:06

UI boot camp playlist this is the

play00:09

original playlist that I put out on my

play00:11

Channel about two years ago now and

play00:13

since then there's been a lot of updates

play00:15

to Swift UI and I thought it was a good

play00:17

time to now revisit this playlist and

play00:19

append a couple videos here of maybe

play00:21

some of the new features that I've not

play00:23

yet covered so I know iOS 16 came out

play00:25

about a year ago now but there's some

play00:27

really cool features in there that I

play00:29

really want to share with you guys I'm

play00:30

going to do that in the next couple

play00:31

videos there's also been a couple things

play00:33

that I've now deprecated since I

play00:35

originally made this playlist I've

play00:37

gotten a lot of comments on this has

play00:39

been deprecated what's the new version

play00:40

and so I'm also going to add on a couple

play00:42

videos here on showing you the new way

play00:44

for some of those things that have been

play00:46

deprecated I'll show you guys the new

play00:47

way to do that and in those videos I'm

play00:50

also going to talk a little bit about

play00:51

why those things have been deprecated

play00:54

because it's not that Apple's just wants

play00:56

to change their API it's that they are

play00:58

making Swift UI better so anything that

play01:00

has been deprecated it's deprecated not

play01:03

because it doesn't really work anymore

play01:04

but because there's a new and better way

play01:06

to do it and so that's what we're going

play01:08

to dive into in the next couple videos

play01:09

this video as you already know though is

play01:12

the navigation Stack video so the the

play01:16

probably the biggest improvement from

play01:17

IOS 16 is the navigation stack prior to

play01:20

iOS 16 we had the navigation view which

play01:23

still works in our code but if we are

play01:26

building Swift UI apps now and we can

play01:27

support iOS 16 or above we should be

play01:30

using a navigation stack over navigation

play01:32

view I'm going to explain this video

play01:34

firstly obviously how to use it but I'm

play01:37

also going to explain some of the

play01:38

benefits of the stack over the view and

play01:41

I'm not kidding when I say that it is

play01:42

incredibly powerful the navigation stack

play01:45

opens us up to so many more features

play01:47

that we didn't previously have so I am

play01:50

super excited to share this with you

play01:51

guys I apologize that I have not covered

play01:54

this sooner there's just been a lot of

play01:56

other videos Firebase videos that I

play01:58

wanted to get through but if you have

play01:59

not learned how to use it yet I hope

play02:01

this video helps somebody I am using

play02:02

navigation stack in all of my apps at

play02:05

this point it is awesome let's check it

play02:07

out

play02:15

welcome back everyone it has been a

play02:18

while since I've added to this playlist

play02:20

I am pretty excited most of the stuff

play02:22

that I've covered in this playlist is

play02:24

still relevant today although Apple has

play02:26

made some pretty major updates in iOS

play02:29

16. I'm sure they're going to make even

play02:31

bigger updates in iOS 17 and I'm a

play02:34

little late to the game for 16 but I do

play02:36

want to still cover them so earlier in

play02:39

this playlist we covered the navigation

play02:41

view boot camp and the navigation view

play02:44

still works but if you're building your

play02:47

app for iOS 16 or above you should

play02:49

basically use navigation stack instead

play02:52

of navigation View

play02:53

navigation stack is the new version

play02:55

navigation view is now deprecated and

play02:59

I'm going to show you in this video how

play03:00

to use navigation stack and I'm also

play03:02

going to highlight some of the changes

play03:03

that make navigation stack actually

play03:05

better than navigation View

play03:07

now I'm still using navigation View and

play03:09

a lot of my own apps I don't think it's

play03:11

going to be a problem that it's in your

play03:13

app for at least the foreseeable future

play03:16

but if you can use navigation stack I

play03:19

would definitely switch it over again

play03:22

I'm back here in the swiffle thinking

play03:23

boot camp the original boot camp on this

play03:25

channel and I'm going to right click the

play03:27

Navigator create a new file it'll be a

play03:30

swift UI View and let's call this one

play03:32

navigation stack boot camp

play03:36

go ahead and click create once we're

play03:39

inside let's get that preview working

play03:42

all right we are going to delete this

play03:45

text and let's add a navigation view the

play03:47

old navigation view that we learned in

play03:49

an earlier video in this playlist and we

play03:52

can see that navigation view it will be

play03:54

deprecated in a future version of iOS so

play03:57

this is Apple telling us that you can

play03:59

still use navigation view but it's going

play04:01

to be devastated so there will be a

play04:03

point of time in the future where you

play04:06

won't be allowed to use it anymore

play04:07

although it probably won't be for a very

play04:10

long time

play04:11

because Apple doesn't want to break the

play04:13

apps that are in the app store today so

play04:15

they are probably going to keep it

play04:17

usable at least for the foreseeable

play04:19

future regardless we should update to

play04:21

navigation stack as soon as possible

play04:22

there also is a navigation split view

play04:25

but I'm not going to cover that I have

play04:26

actually never used it I think it's more

play04:28

common on like iPads and larger devices

play04:30

but navigation stack has a lot to offer

play04:33

and is what I'm using in pretty much all

play04:35

of my apps all of my apps right now but

play04:38

navigation stack is the go-to and

play04:40

probably the one that you should be

play04:41

using in most if not all of your apps

play04:44

but we're gonna start right now with the

play04:46

navigation View and look at how it's set

play04:48

up before we get to the navigation stack

play04:50

so navigation view puts us into a

play04:53

navigation hierarchy and then once we're

play04:55

in that hierarchy we can add some text

play04:56

here let's say hello world

play04:59

and because we're in the navigation

play05:01

hierarchy we can add that navigation

play05:03

title and say maybe nav

play05:08

boot camp

play05:10

right and so we can't get this

play05:11

navigation title we can't get the nav

play05:13

bar at the top unless we're in a

play05:16

navigation view this should not be new

play05:17

to you guys if you followed this

play05:19

playlist I we've been using this in many

play05:21

many videos already

play05:22

so when we're in a navigation view in

play05:24

order to push to the next screen we use

play05:27

a navigation link

play05:28

so I'm going to add one here real quick

play05:30

so I'll say navigation link

play05:33

open the parentheses I'm going to go

play05:35

with the destination and label

play05:37

completion here

play05:40

the destination let's just say screen

play05:42

two

play05:44

and the label let's just say text that

play05:47

says maybe click me

play05:50

all right so I've got it on our screen

play05:52

here if I click it it pushes to the next

play05:54

screen and we can go backwards this is

play05:56

our standard Behavior

play05:58

and before we move forward I want to

play05:59

talk a little bit about some of the

play06:01

limitations that are in this current

play06:03

setup here so first and foremost this

play06:06

navigation link allows us to push

play06:07

forward one screen and one screen only

play06:10

so I can go to the next screen but I

play06:13

can't go to screen three four I can't

play06:16

push multiple screens at the same time

play06:18

navigation stack we're going to be

play06:20

allowed to do that

play06:21

the probably bigger and more important

play06:24

thing that navigation stack allows us to

play06:25

do is have basically lazy loading on

play06:28

these secondary screens

play06:30

so some of you might not understand what

play06:32

lazy loading is so I'm going to just do

play06:34

a quick example of this so first I'm

play06:36

going to create a struct for the second

play06:38

screen the second screen that is

play06:40

currently screen two I'm just going to

play06:42

create another struct that says my

play06:44

second screen

play06:46

it'll be a view and we'll open the

play06:48

brackets every view needs a body and

play06:50

let's just put a text screen two in here

play06:54

and actually let's pass in a integer

play06:58

we'll say value of type int

play07:01

and I'm gonna just for now I'm going to

play07:03

create a custom initializer here

play07:05

and inside this initializer I'm just

play07:07

going to print out the screen that is

play07:09

being initialized so I'm going to print

play07:11

out init init for screen

play07:15

and then let's just pass in the value so

play07:18

this should print out a knit screen one

play07:20

two three Etc and we'll just put that

play07:23

value on the screen just so we know

play07:24

we're going to the right screen

play07:26

all right so if I put in here my second

play07:29

screen with a value of one

play07:32

click this it goes to my screen one and

play07:36

let's real quick do a four each and

play07:39

we're going to do a four each Loop so

play07:40

let's do zero to maybe less than ten of

play07:43

these and this will be X and I'm just

play07:46

going to put the navigation link inside

play07:49

so here we're going to create a very

play07:50

simple Loop here on 10 items each with a

play07:54

navigation link and this way each of

play07:56

those items we will go to the X screen

play08:01

so let's render this real quick

play08:06

let me stop the preview let me start it

play08:08

up again

play08:10

and they're not loading because we are

play08:13

not in a v-stack do a v-stack here

play08:16

actually let's do a scroll view followed

play08:18

by a v stack and here we go

play08:22

all right add some spacing of maybe 40.

play08:26

all right so I got all these buttons

play08:27

here click me actually put click me with

play08:30

the X here

play08:32

oh man getting sloppy Nick all right

play08:35

click me zero goes to zero click me six

play08:38

goes to six this is standard Behavior

play08:40

what I'm going to do now is take this

play08:42

and make this the first screen in my app

play08:48

all right and I'm gonna build and run

play08:50

this to the simulator and before I even

play08:52

click anything I'm just going to look at

play08:54

what prints out to my console and I can

play08:57

see here that we have the init for all

play08:59

of these screens already created so I

play09:02

haven't clicked any of these yet but the

play09:04

initializer for screen 789 has already

play09:08

ran now it ran twice for each of these

play09:11

and I'm not concerned that I ran twice

play09:12

because that's just how the view renders

play09:14

but what I'm concerned about is the fact

play09:17

that I'm only on this screen and yet all

play09:21

nine of these screens have been rendered

play09:23

in the background

play09:25

if you follow my crypto app or if you've

play09:28

been building any apps pretty much prior

play09:30

to iOS 16 you probably ran into this

play09:33

problem because these navigation links

play09:34

are not lazy and so if we had a lot of

play09:37

maybe this second screen here was like a

play09:39

big heavy lifting screen if that had a

play09:42

lot of like download functions or

play09:44

loading functions we're literally

play09:46

loading nine extra screens behind the

play09:49

scenes right now and you can imagine on

play09:52

a larger app right this could actually

play09:54

slow down the entire app tremendously we

play09:58

only want to load the screens and the

play10:00

things that we need to load when we need

play10:02

to load them

play10:03

and so one of the major benefits of

play10:05

moving to a navigation stack is the

play10:07

ability to do this in a lazy fashion so

play10:10

that we only load the screen when we go

play10:12

to it

play10:13

and now let's actually jump in and look

play10:16

at the navigation stack

play10:18

so to use a navigation stack is actually

play10:20

quite simple all we need to do is use a

play10:22

navigation stack instead of a navigation

play10:25

View

play10:26

and we can actually still use these

play10:28

navigation links as is

play10:32

and it looks like navigation stack is

play10:34

only available on iOS 16. I'm just going

play10:37

to make my whole project only compile

play10:39

for iOS 16 now

play10:42

just because this is a boot camp

play10:45

and here we go

play10:47

let's put this on the simulator again

play10:50

and we should see that it still works

play10:52

awesome I'm gonna build and run this to

play10:54

the simulator again and we do still have

play10:56

this initializer problem because again

play10:58

these navigation links are not lazy but

play11:02

it is good to know that at least that as

play11:05

we start upgrading our app we can still

play11:07

use this existing setup without needing

play11:10

to refactor everything right now right

play11:13

so if you have this in your app you can

play11:16

keep that code as is and you can just

play11:17

change the navigation stack and

play11:20

gradually start shifting your navigation

play11:21

links to navigation destinations

play11:27

all right so firstly I'm just going to

play11:29

move the navigation title down here

play11:30

because I don't really want to work with

play11:32

it

play11:33

and so all right Nick show us what a

play11:36

navigation destination is well it's just

play11:38

another modifier we'll call navigation

play11:40

destination

play11:43

and we're going to be working with the

play11:45

four destination completion here I think

play11:48

it's much more common than the other one

play11:50

and it's going to allow us to go to a

play11:52

destination view for a certain data type

play11:56

now that data type is going to be pretty

play11:57

important so I'm going to hold the

play11:59

option button and click enter here

play12:00

navigation destination for what type any

play12:04

type that conforms to the hashable

play12:05

protocol

play12:06

well most types by default already

play12:09

conform to hashable so if you're making

play12:11

your own model you have to conform to

play12:13

hashable but we're right now are just

play12:14

using integers so I'm just going to use

play12:16

integer dot self and again self is

play12:20

referring to the type of integer the

play12:23

type integer destination is basically

play12:27

where we want to go and obviously we

play12:28

want to go to my second screen so I'll

play12:30

click enter here

play12:31

this hashable value is the selected

play12:34

value of this type so if we say we're

play12:37

going to go to integer number four

play12:38

that's the number that's going to come

play12:40

through here so I'm just going to call

play12:41

this the value

play12:43

and then we'll just go to my second

play12:45

screen and I'll pass in value

play12:53

and now we're going to do is instead of

play12:55

using this navigation link with a

play12:57

destination and a label we're going to

play12:59

use just a very simple we're just going

play13:01

to use a different initializer here

play13:02

navigation link

play13:04

we're going to look for the one with a

play13:06

value again this value is hashable this

play13:09

value this corresponding value is the

play13:12

same value that we are putting down here

play13:14

so navigation link with a value and what

play13:18

is the value of this navigation link so

play13:20

when we click on this label so it's

play13:22

going to be the click me the value is

play13:24

going to be that X right the value that

play13:26

we are passing in so we'll just put here

play13:28

x

play13:29

that again that in that X is an integer

play13:32

right so we're looking for integers down

play13:35

here this all needs to line up with the

play13:37

same type and the label we'll just put

play13:40

this here

play13:42

and let's let this compile real quick so

play13:45

just a slight change to our code it

play13:47

looks like it's the exact same in the UI

play13:50

here if I click on three we go to three

play13:52

if I click on seven we go to seven but

play13:54

this time I'm going to build and run

play13:56

this to the simulator

play13:57

and I'm going to open up the console

play14:00

here and what do you know nothing is

play14:02

printed out to the console so it looks

play14:05

the exact same but behind the scenes

play14:07

there's been a tremendous difference

play14:09

right we did not load all nine of these

play14:12

screens already so if I click on four

play14:14

it's going to load four at that time if

play14:16

I click on seven it's gonna Load 7 at

play14:18

that time and just this by itself is

play14:21

enough for us to use navigation stack

play14:23

over navigation view this is I think

play14:27

underratedly the biggest advancement to

play14:29

Swift UI and iOS 16. this lazy loading

play14:33

of navigation is incredibly important

play14:37

for production level scalable apps so if

play14:41

you're not doing this in your apps I

play14:43

highly recommend this is like the first

play14:44

thing that probably should be refactored

play14:48

and this is awesome but of course

play14:50

there's way more that we can do with the

play14:51

navigation stack

play14:53

so let's play around with this a little

play14:55

bit up here I'm going to add a let's

play14:58

just say maybe let fruits of type it's

play15:00

going to be an array of string and let's

play15:02

just add in apple let's add in the

play15:04

orange

play15:07

and let's add in everybody's favorite

play15:08

b-a-n-a-n-a-s

play15:11

without the S because there's only one

play15:13

banana on this list

play15:15

and

play15:17

this is an equal sign and here we go all

play15:19

right so we got an array of fruits let's

play15:21

loop on these fruits as well so I'm

play15:23

going to do it maybe let's do it above

play15:25

these let's just put it here for each

play15:28

let's loop on the fruits

play15:31

fruits are not identifiable but they are

play15:33

hashable so we need to add in that ID so

play15:36

that we can tell it where the hash value

play15:38

is the hash value is self which is the

play15:41

string itself and here we go this will

play15:45

be each fruit and what do we want to add

play15:47

here I'm going to add in another

play15:49

navigation link

play15:51

this time again I'm going to use the

play15:53

value label value I'm going to put in

play15:55

the fruit and the label let's just put

play15:57

in text fruit

play16:00

let's just put in text and let's just

play16:02

add in the fruit

play16:03

why not let's refresh the canvas we got

play16:06

Apple orange banana on the screen but

play16:10

if I click it it's not going to go

play16:13

now before I tell you guys exactly why

play16:16

take a second look at this code and try

play16:18

to debug it yourself try to figure out

play16:20

why is the The Click me one two nine are

play16:25

working perfectly but the orange and

play16:27

banana are not

play16:31

so pause it if you need to but if not

play16:33

I'm going to keep moving here so the

play16:35

reason is because when we edit a

play16:37

navigation destination we told it look

play16:39

for values that are integers and anytime

play16:42

you get an integer go to this screen but

play16:46

now these fruits are strings right so

play16:49

they're no longer integers and so

play16:51

because they're strings we actually need

play16:53

to add another navigation destination

play16:55

what destination are we going to for

play16:58

Strings

play17:00

and so I'm just going to put down here I

play17:02

don't think we need to create another

play17:03

second screen I'm just going to put in

play17:04

some text that says another screen

play17:08

and let's put in the the value of the

play17:10

fruit

play17:11

and of course this is going to be a

play17:13

string

play17:14

it's so it's really cool and kind of

play17:15

unexpected as a developer that we can

play17:17

actually stack these navigation

play17:19

destinations we can literally have

play17:21

multiple destinations for multiple

play17:23

different data types and this again it

play17:26

really helps us for scalable apps

play17:27

because you might have you know a user

play17:30

profile model you might have a post

play17:32

model you might have a you know settings

play17:35

screen and so now we can really start to

play17:38

make our screens that our routing at

play17:40

least Dynamic here so I added this

play17:42

string destination and just like that I

play17:45

can now go to the orange screen the

play17:47

banana screen and I can also go to the

play17:50

integer screens

play17:52

so again this is just a huge step up

play17:53

from the capabilities of the navigation

play17:55

View

play17:57

all right the last thing I'm going to do

play17:59

before we wrap up this video is talk

play18:00

about the path that we can bind to in a

play18:04

navigation stack so when you build out a

play18:06

navigation stack there actually is a

play18:10

path binding here and we can see that

play18:12

the path is of type binding to a

play18:15

navigation path

play18:16

the navigation path is a little

play18:18

misleading because a navigation path is

play18:21

pretty much anything that is hashable so

play18:24

for example down here our navigation

play18:25

path is an integer here our navigation

play18:28

path is a string so it's a little

play18:30

confusing the first time you use it but

play18:31

really it's just a binding and then we

play18:34

have our closure so I'm just going to

play18:37

delete this bit of code here and use

play18:39

this path and what are we going to bind

play18:41

to so let's create up here and add State

play18:43

private VAR we'll call this stack path

play18:47

of type and let's just do it with the

play18:49

strings for a second because we already

play18:51

have our fruit array let's just say bind

play18:53

to an array of string

play18:55

and we'll set it equal to a blank array

play18:58

I'm just going to bind to it with the

play19:00

money sign stack path

play19:02

all right and now it seems like we

play19:04

didn't add much code but this did change

play19:07

the actual usage of our app pretty

play19:09

dramatically already

play19:11

so this navigation stack meaning the

play19:13

entire stack is now set up to bind to

play19:18

destination values for Strings there's

play19:22

only strings in here there are not

play19:24

integers in here so even though we have

play19:26

the destination of an integer and we

play19:29

have the destination of a string of

play19:30

course this integer one is now broken

play19:32

because the the path is overriding any

play19:36

of the other destinations so if I click

play19:38

on Orange it's going to work because

play19:40

those are strings if I click on number

play19:42

four it's now broken and just to prove

play19:45

that if I again if I take out this path

play19:48

and then click on four it works if I put

play19:50

the path back in and I click on four

play19:52

it's broken so this is pretty good to

play19:55

know it's not a bug that's just how it

play19:57

works

play19:59

but why would we want to do a stack path

play20:01

if it's going to limit Us in that

play20:03

direction well because when we buy the

play20:05

stack path we can now programmatically

play20:07

append to this path so when we click on

play20:11

a string it's actually just adding that

play20:14

string to this path so if I click on

play20:17

banana basically the stack path is going

play20:19

to be an array of one item that has a

play20:21

string of banana in it but what I'm

play20:24

going to do up here is actually create

play20:25

my own button that says maybe a super

play20:29

Segway

play20:30

and the action I'm going to call stack

play20:33

path

play20:34

dot append and I'm going to append an

play20:37

element let's just say maybe coconut

play20:41

all right so here I'm going to Super

play20:43

segue and it binds to Coconut but why do

play20:47

I call it super Segway because what we

play20:49

can actually do is append

play20:51

an array to this path so I'm going to

play20:55

append contents of another array

play20:57

and here I'm going to put in coconut

play20:59

let's do watermelon

play21:03

and and let's do maybe mango

play21:07

so this is insanely cool now we were not

play21:09

able to do this in iOS 15 but navigation

play21:12

stack allows us to now push to multiple

play21:15

screens at the same time

play21:17

so what this is going to append all

play21:19

three of these to the stack basically

play21:21

pushing forward three screens at once

play21:25

super Segway I'm on Mango Mango is the

play21:28

third one and so now when I click back

play21:30

it's not going to go back to that

play21:31

original screen it's going to first go

play21:33

to watermelon then it's going to go to

play21:35

coconut and then we're back

play21:37

and so if you're just starting you're

play21:39

building your basic apps this is

play21:41

probably not a real use case that you're

play21:44

going to have very often but

play21:47

but larger scalable apps bigger

play21:49

companies are going to need this setup

play21:51

this allows you to push multiple screens

play21:53

at once into the path

play21:56

and there are absolutely use cases where

play21:58

this is like a crucial part of some

play22:00

applications

play22:02

so that is all I have for you guys for

play22:04

the navigation stack there are ways to

play22:06

kind of override and customize this

play22:09

stack path so that it can accept

play22:11

multiple data types but it's much more

play22:13

advanced it's a little hackier and I

play22:15

don't want to get into it in this

play22:16

playlist so for right now I think this

play22:18

covers pretty much all of your use cases

play22:20

for your beginner intermediate level

play22:22

apps generally I do not use the the path

play22:25

unless I need to do this super segue

play22:29

basically and most apps honestly don't

play22:32

need to do that so I've been avoiding it

play22:34

but if you have that binding there you

play22:35

can then do that super segue but one

play22:37

other cool thing that that allows you to

play22:39

do is actually initialize the view with

play22:42

a preset binding if you wanted to so you

play22:45

could do something like an on a pier or

play22:47

you could even just put it up here you

play22:49

could just start with maybe you know one

play22:52

to

play22:54

three

play22:55

and so as soon as this screen loads it's

play22:57

actually starting on the third screen

play22:59

already you can really get pretty

play23:01

customizable with this stuff

play23:03

but that is it for me bottom line here

play23:06

is if you're using navigation view you

play23:08

can still use it but get on a path to

play23:12

refactoring to the navigation stack it's

play23:14

just it's better it's newer it's more

play23:16

performant and it's honestly easier to

play23:19

use because if you follow my crypto app

play23:21

we did a lot of work to kind of create

play23:23

our own custom lazy navigation and I'm

play23:28

not going to go and change that that

play23:29

course right now I think it's just like

play23:31

one screen that we really did that on

play23:32

but this allows us to avoid all that

play23:34

hackiness completely by just giving us

play23:37

lazy navigation destinations out of the

play23:39

box

play23:41

all right thank you guys for watching as

play23:44

always I'm Nick this is swiftful

play23:45

thinking and I'll see you in the next

play23:47

video

play23:50

[Music]

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Swift UIiOS 16导航栈懒加载多屏幕推送教程更新Swiftful ThinkingNick编程技巧用户界面应用开发
هل تحتاج إلى تلخيص باللغة الإنجليزية؟