Create a List of Todo items in SwiftUI | Todo List #1

Swiftful Thinking
14 Mar 202120:53

Summary

TLDR在这段视频中,Nick介绍了如何使用MVVM架构来构建一个待办事项列表应用程序。首先,他解释了MVVM代表的含义,即模型(Model)、视图(View)和视图模型(ViewModel),并强调了视图(View)是用户界面逻辑的核心部分。接着,Nick展示了如何在Swift UI中构建两个主要的视图:一个是显示待办事项列表的视图,另一个是允许用户输入并添加新事项的视图。他通过创建一个新的Xcode项目,详细介绍了如何设置项目并开始编码。在构建过程中,Nick创建了一个名为“listView”的新视图,并在其中添加了一个列表,用于展示待办事项。此外,他还创建了一个“listRowView”组件,用于显示每个待办事项的复选框和标题。最后,Nick展示了如何添加导航栏按钮,包括一个编辑按钮和一个用于添加新事项的按钮,并简要介绍了如何构建一个简单的添加视图。视频以一个完整的待办事项列表和添加新事项的界面作为结束,为接下来的视频内容——深入探讨模型和待办事项数据——奠定了基础。

Takeaways

  • 📝 视频中介绍了使用MVVM架构构建待办事项应用程序,MVVM代表模型(Model)、视图(View)、视图模型(ViewModel)。
  • 🔍 第一个'V'代表视图(Views),视频中将构建应用程序中的两个主要视图:一个用于显示待办事项列表,另一个用于输入和添加新事项。
  • 🚀 通过SwiftUI组件直接在屏幕上添加内容,使得这个视频成为课程中相对简单的一个部分。
  • 💻 推荐使用Xcode 12.3或更高版本进行开发,但不是必须使用最新版本。
  • 📱 创建一个新的Xcode项目,选择常规iOS应用,专门用于iPhone和iPad。
  • 📍 在SwiftUI中,使用NavigationView将整个应用包裹起来,以便于导航。
  • 📋 构建了一个ListView,它是一个列表,用于显示待办事项,并且可以添加导航标题和图标。
  • 🔑 通过创建一个名为ListRowView的自定义组件来显示每个待办事项,包括复选框和事项标题。
  • 🔄 为了代码的清晰和分离,将ListRowView组件分离到单独的文件中。
  • 🗂 使用状态变量(State)来管理待办事项列表中的数据,而不是直接在视图中硬编码。
  • ➕ 添加了一个Add View,用于输入新的待办事项,包括一个文本框和一个保存按钮。
  • ✅ 通过使用NavigationViewLink实现了从ListView到Add View的导航,以及返回按钮的自动生成。

Q & A

  • 什么是 MVVM 架构?

    -MVVM 架构包含三个部分:Model、View 和 ViewModel。Model 是数据点,View 是应用程序的 UI 组件,ViewModel 则负责管理 View 所需的数据,将 Model 数据绑定到 View 上。

  • 在 MVVM 架构中,ViewModel 的作用是什么?

    -ViewModel 的作用是管理数据,为 View 提供数据,并处理与用户界面交互相关的逻辑。它负责将 Model 数据转化为适合在 View 上显示的格式,并与 View 进行双向数据绑定。

  • 在视频中,为什么要将整个应用程序都包裹在 NavigationView 中?

    -将整个应用程序包裹在 NavigationView 中是为了确保应用的每个视图都处于导航视图的环境中,这样可以轻松实现视图之间的导航和页面跳转。

  • 为什么在 List 视图中使用了 Spacer?

    -在 List 视图中使用 Spacer 是为了将列表项内容推到屏幕的左侧,使其左对齐。这样可以使列表项看起来更整齐、更规范。

  • 为什么在 List 视图中创建了一个名为 listRowView 的组件?

    -在 List 视图中创建 listRowView 组件是为了使列表项的样式和结构可重用,这样可以使代码更清晰、更模块化,也方便后续的维护和扩展。

  • 为什么在 AddView 中使用了 ScrollView?

    -在 AddView 中使用 ScrollView 是为了确保当用户输入内容超出屏幕可视范围时,能够滚动查看所有内容。这样可以保证用户在输入较长内容时能够方便地进行操作。

  • 为什么在 AddView 的按钮上使用了 NavigationLink?

    -在 AddView 的按钮上使用 NavigationLink 是为了实现按钮点击后的页面跳转功能,将用户导航回列表视图,从而实现添加新项目后的导航流程。

  • 为什么在 List 视图中使用了 @State 属性包裹 items 数组?

    -在 List 视图中使用 @State 属性包裹 items 数组是为了在视图中引入数据,并使其能够随着用户交互而动态更新。这样可以确保视图与数据的同步性。

  • 在 List 视图中,为什么要使用 @State 属性包裹 textFieldText 字符串?

    -在 List 视图中使用 @State 属性包裹 textFieldText 字符串是为了创建一个与文本字段绑定的可变状态,以便在用户输入时更新文本字段的内容。这样可以确保文本字段的动态更新。

  • 为什么在 AddView 的按钮上使用了 .frame(maxWidth: .infinity)?

    -在 AddView 的按钮上使用 .frame(maxWidth: .infinity) 是为了让按钮的宽度自适应父视图的宽度,使其填充整个可用空间。这样可以确保按钮在不同屏幕尺寸下的一致性表现。

Outlines

00:00

📱 开始构建待办事项应用的视图

在本段中,Nick介绍了将使用MVVM架构来构建待办事项应用。MVVM代表模型-视图-视图模型,其中视图负责应用中的所有UI逻辑。Nick计划在视频中构建应用的两个主要视图:一个用于显示待办事项列表,另一个用于输入并添加新事项。他还提到了使用Xcode 12.3版本,并建议使用12.0或更高版本。最后,Nick指导如何创建一个新的Xcode项目,并解释了MVVM架构的基本概念。

05:01

🔍 创建列表视图和行视图组件

Nick继续指导如何在项目中创建一个新的'views'组,并在其中创建一个Swift UI视图文件,命名为'listView'。他解释了在Swift UI中创建列表视图的过程,包括添加导航标题和使用系统图标。Nick还展示了如何创建一个可重用的'listRowView'组件,用于显示待办事项列表中的每个条目,并将其转换为单独的文件以保持代码的整洁。

10:01

🧱 构建添加新事项的视图和导航栏

在这一段中,Nick开始构建用于添加新事项的视图。他首先创建了一个名为'addView'的新Swift UI视图,并在其中嵌入了一个滚动视图。接着,他添加了一个文本字段,允许用户输入事项的标题,并使用了一个绑定的字符串变量'textFieldText'。此外,Nick还为添加视图创建了一个保存按钮,并设置了其样式和属性。最后,他更新了'listView'中的导航链接,以便点击添加按钮时能够导航到新创建的'addView'。

15:03

🎨 设计和格式化文本字段和按钮

Nick专注于设计和格式化文本字段和保存按钮。他为文本字段添加了背景颜色、高度、圆角和内边距,以提高其视觉效果。同时,他还为保存按钮设置了背景颜色、文字颜色、高度、最大宽度和圆角,以及字体样式,确保按钮与文本字段在视觉上保持一致。

20:05

🔄 完成视图设置并准备进入下一阶段

在视频的最后部分,Nick完成了对列表视图和添加视图的设置。他展示了如何通过点击添加按钮在列表视图和添加视图之间导航,并强调了在接下来的视频中将开始处理模型,以便使用更丰富的数据结构来管理待办事项列表。

Mindmap

Keywords

💡MVVM

MVVM是Model-View-ViewModel的缩写,是一种软件架构模式,用于构建和组织应用程序。在视频中,MVVM用于指导开发一个待办事项列表应用程序。Model代表数据模型,View代表用户界面,ViewModel负责管理数据和业务逻辑。MVVM有助于分离应用的各个部分,使代码更易于维护和测试。

💡SwiftUI

SwiftUI是一个由苹果公司开发的用户界面工具包,允许开发者使用Swift语言创建iOS、macOS、watchOS和tvOS的用户界面。在视频中,SwiftUI被用来构建应用程序的视图,包括列表视图和添加视图。

💡Xcode

Xcode是苹果公司开发的集成开发环境(IDE),用于开发macOS、iOS、watchOS和tvOS的应用程序。视频中提到使用Xcode 12.3版本来创建和管理项目,Xcode提供了编写代码、界面设计和应用测试等工具。

💡Navigation View

在SwiftUI中,NavigationView是一个容器,用于提供导航功能,允许用户在不同的视图之间导航。视频中提到将整个应用程序包裹在NavigationView中,以确保所有视图都具备导航功能。

💡List

List是SwiftUI中的一个视图,用于显示一个滚动的项目列表。在视频中,List被用来展示待办事项列表,每个待办事项作为一个列表项显示。

💡HStack

HStack是SwiftUI中的一个布局视图,代表水平堆栈,用于水平排列子视图。在视频中,HStack被用来创建每个待办事项的行视图,包含复选框和标题。

💡List Row View

List Row View是视频中自定义的一个SwiftUI视图,用于表示待办事项列表中的单个行。它通过提取HStack中的子视图来创建,使得代码更加清晰和可重用。

💡State Variable

State Variable是在SwiftUI中用于存储随时间变化的数据的变量。在视频中,创建了一个状态变量`items`来存储待办事项列表的数据。状态变量允许视图响应数据的变化并更新UI。

💡NavigationView

NavigationView是SwiftUI中的一个视图,它提供了一个导航栏和导航功能,允许用户在不同的视图之间进行导航。在视频中,NavigationView用于为待办事项列表和添加视图提供导航结构。

💡Text Field

Text Field是SwiftUI中的一个视图,允许用户输入文本。在视频中,Text Field被用来创建一个输入框,用户可以在此输入新的待办事项标题。

💡Button

Button是SwiftUI中的一个视图,用于创建用户可以点击的按钮。在视频中,Button被用来创建一个“保存”按钮,用户点击此按钮可以提交新的待办事项。

Highlights

Nick介绍了如何使用MVVM架构构建待办事项应用,MVVM代表模型(Model)、视图(View)、视图模型(ViewModel)。

在MVVM中,视图(View)包含所有UI逻辑,与用户直接交互的屏幕或组件都属于视图。

视频中将构建应用的两个主要视图:一个用于显示待办事项列表,另一个用于输入新事项。

Nick推荐使用Xcode 12.3版本进行开发,但建议至少使用12.0或更高版本。

创建新的Xcode项目时,选择常规iOS应用,并确保使用SwiftUI界面和SwiftUI应用生命周期。

在项目中创建了名为'views'的组,并在其中创建了名为'listView'的SwiftUI视图文件。

为了保持代码整洁,将'listRowView'组件提取为单独的文件。

使用状态变量'items'来存储待办事项列表的数据,初始为字符串数组。

在列表中使用了ForEach循环来动态生成待办事项列表项。

设置了导航栏标题,并使用了备忘录表情符号作为图标,增强了UI的视觉效果。

为待办事项列表项创建了自定义的'listRowView'组件,以提高代码的可重用性。

添加了导航栏按钮,包括一个编辑按钮和一个用于添加新事项的导航链接。

创建了名为'addView'的新视图,用于输入和添加新的待办事项。

在'addView'中使用了滚动视图(ScrollView)和文本字段(TextField),以及保存按钮(Button)。

对文本字段进行了样式设置,包括背景颜色、边框、圆角和内边距。

设计了保存按钮,包括文字样式、背景颜色和边框圆角。

通过点击'add'按钮,可以在待办事项列表和添加新事项视图之间进行导航。

视频结束时,Nick预告了下一期视频将开始探讨模型(Model),以使用更丰富的待办事项数据结构。

Transcripts

play00:00

[Music]

play00:06

welcome back everyone

play00:07

i'm nick this is swiffle thinking and as

play00:10

i mentioned we're going to build

play00:11

this to-do list app with mvvm

play00:14

architecture

play00:15

and that stands for model view view

play00:17

model now it's okay if you don't know

play00:19

what any of that means yet

play00:20

but the first v in mvvm stands for views

play00:23

and that's what we're going to build in

play00:25

this video so in the views

play00:27

should be all of the ui logic in your

play00:30

app

play00:31

so anything relating to a screen or a

play00:33

component that a user is actually

play00:35

touching that goes into the view and in

play00:38

this video we're going to build the two

play00:39

main views in our app

play00:41

which the first few is going to hold a

play00:43

list of to do items

play00:44

because this is a to-do list app and the

play00:46

second view is going to have a little

play00:48

area where we can actually type

play00:49

something in

play00:50

and add a new item to our to-do list so

play00:53

this is going to be one of the easier

play00:54

videos in the course because it is

play00:55

so straightforward we're just adding

play00:57

swift ui components to the screen

play00:59

and by the end of this video you'll have

play01:02

a good idea of what our app is going to

play01:04

look like

play01:04

so that's enough of me talking let's get

play01:07

coding

play01:09

all right so i am on my computer here

play01:12

and i'm gonna open up

play01:13

xcode if you don't have xcode on your

play01:16

computer and you don't know what xcode

play01:18

is

play01:18

i highly recommend going to check out

play01:20

the swift ui bootcamp which is another

play01:22

series i have on my youtube channel

play01:24

and in the first couple of videos of

play01:26

that series we downloaded xcode

play01:28

installed it and then went through some

play01:30

of the basics and how to use it so

play01:32

if you're taking this course i'm going

play01:33

to assume you already have the basics of

play01:35

xcode down

play01:36

because i did provide that in the swift

play01:38

ui bootcamp

play01:39

and with that said i'm going to be using

play01:41

version 12.3 for this course

play01:43

now it's not crucial that you have the

play01:45

most updated version of xcode but i

play01:47

would recommend using version 12.0 or

play01:49

higher and let's get started by creating

play01:52

a new xcode project

play01:56

it's going to ask us what type of

play01:58

project we want to create and this is

play01:59

going to be a fairly simple app

play02:01

so we're just going to use a regular app

play02:03

and we're going to make sure that it's

play02:04

on the ios tab

play02:06

because we're going to build our app

play02:07

just for the iphone and ipad which is

play02:09

ios

play02:10

we're not going to do mac watch or tv in

play02:12

this video

play02:13

so regular ios regular app double click

play02:17

it or click next

play02:18

then it's going to ask you for a product

play02:20

name and this is just the name of your

play02:22

app

play02:22

i'm going to call it to do list in camel

play02:25

case

play02:26

and i find it best practice to not

play02:28

include spaces in the product name

play02:30

so i'm doing to-do list but i'm making

play02:32

it one word with no spaces

play02:35

your team if you have a apple developer

play02:37

account or a professional developer

play02:38

account

play02:39

that will be up in the team here it's

play02:41

not crucial that you have one so it's

play02:43

okay if it says none

play02:44

and then the organization identifier

play02:46

again not super important because we're

play02:48

not putting this into the app store

play02:50

but it is best practice to use com dot

play02:52

and then either

play02:53

your name or the name of your company or

play02:56

your

play02:57

organization whatever you want to call

play02:59

it and then it will create a

play03:01

bundle identifier for this project was

play03:03

just a unique key for this project

play03:05

and so it's going to be your

play03:06

organization identifier dot your product

play03:09

name the important thing on this screen

play03:11

is that we are using a swift ui

play03:13

interface

play03:14

we are using a swift ui app life cycle

play03:17

and the language of course is swift

play03:20

in this project we are not going to use

play03:21

core data and we are not going to use

play03:23

tests so you can

play03:24

uncheck these if they are checked and

play03:26

let's go ahead and click

play03:27

next

play03:31

go ahead and find the folder where you

play03:32

want to save it on your computer

play03:34

and go ahead and click create

play03:38

and now that we're in our project i'm

play03:39

going to make it full screen so you guys

play03:41

could see it a little bit

play03:42

better and let's click resume on the

play03:45

canvas so we know we're all connected

play03:47

i'm going to hide the inspector on the

play03:49

right side because we're not going to

play03:50

use it

play03:54

and this should be nothing new to you

play03:56

guys xcode starts us with a project a

play03:58

very simple project where we have

play04:00

our our app.swift file and a content

play04:03

view now as i mentioned we're going to

play04:06

use mvvm

play04:07

architecture in this app so before we do

play04:10

any coding i want to just quickly run

play04:11

through what that means

play04:13

if you are unfamiliar so i'm going to

play04:15

jump into the app

play04:16

to to-do list app.swift file and we're

play04:19

going to leave some quick

play04:20

comments so i'm going to do a multi-line

play04:22

comment with the backslash and asterix

play04:25

and we're just going to write mvvm

play04:27

architecture

play04:30

and the first m stands for model and

play04:32

model is

play04:33

a data point basically the v

play04:36

is a view and this is the ui of our app

play04:40

and then the vm is view model

play04:43

and this is the observable object or the

play04:46

class

play04:47

that manages the models or the data

play04:50

for the view so manages

play04:53

models for view and

play04:56

there are a ton of resources online if

play04:58

you just google mvvm architecture or

play05:00

what is mvvm

play05:02

that will go into detail about what this

play05:05

is

play05:05

so i'm not going to dive into detail

play05:07

because there is stuff out there

play05:08

but on a very basic level the model are

play05:12

the data points

play05:13

the view is the actual ui components the

play05:15

swift ui code

play05:17

the view model ties it all together this

play05:19

is where all of our data is managed

play05:21

so with that said we're going to start

play05:23

building this out

play05:25

and the first thing we're going to

play05:26

tackle is the view

play05:28

so right now on the left side in the

play05:30

project navigator let's

play05:31

right click it let's create a new group

play05:34

and we're going to call this

play05:36

views so just like we have

play05:39

model view and view model we're going to

play05:40

have model view view model folders

play05:43

and in this first views i'm going to

play05:45

right click on the folder

play05:46

create a new file it's going to be a

play05:49

swift ui view

play05:51

and we're going to start building out

play05:52

our first view this is going to be

play05:53

called

play05:54

list view go ahead and click create

play05:59

it's going to create a new file we've

play06:01

done this many times if you follow the

play06:03

swift ui bootcamp

play06:04

click resume on the canvas just so you

play06:07

know we're all connected

play06:08

and because we're going to be using this

play06:10

list view we actually don't need

play06:12

this content view so i'm going to right

play06:14

click on the content view

play06:15

and just delete it from our app that's

play06:17

the default view that just came with our

play06:19

blank project i'm going to move it to

play06:21

the trash because we are not going to

play06:22

need it

play06:24

and then i'm going to jump into to-do

play06:25

list app.swift file

play06:28

and in this file the first view that our

play06:31

app is looking for

play06:32

is that content view which we just

play06:33

deleted so we can actually change this

play06:36

out for

play06:36

a list view which will be the first

play06:39

screen in our app

play06:41

now i want our entire app to be wrapped

play06:43

in a navigation view

play06:45

so we could add that navigation view

play06:47

directly in the list view code

play06:50

but because i want to make sure that the

play06:51

entire app is always in a navigation

play06:53

view

play06:54

i'm going to actually add it to this

play06:56

file so before we add

play06:58

the list view let's just add a

play07:00

navigation

play07:01

view open the brackets and then we're

play07:04

just going to put the list view inside

play07:06

so now we know that all the views in our

play07:09

app are automatically

play07:10

always going to be within a navigation

play07:12

view

play07:13

let's jump into the list view and start

play07:15

doing some coding here

play07:18

and when we're back in this file we

play07:20

might have to click resume to

play07:21

reconnect it and we know that when we

play07:25

get to this list view we are going to

play07:26

already be inside a navigation view

play07:29

so to make the preview look the same as

play07:31

it would in our app

play07:33

let's put the preview inside a

play07:34

navigation view as well so down here

play07:36

i'm going to add navigation view

play07:40

open the brackets and put our list view

play07:42

inside just so that we have the same

play07:44

environment

play07:45

as in real time when we're running our

play07:47

app we click resume on the canvas one

play07:50

more time

play07:53

so the main object on this list view is

play07:55

of course going to be a list

play07:57

and let's add a list by typing list open

play08:00

the brackets

play08:01

and for now let's just add some text in

play08:03

here and we'll just say hi

play08:04

just so we can get a list going you'll

play08:07

notice that the list starts a little

play08:08

lower on our screen

play08:09

and that's because we have room for the

play08:11

navigation bar because we're in a

play08:12

navigation view

play08:14

so let's set that up quick let's add a

play08:16

navigation title so underneath the list

play08:19

dot navigation title and i'm going to

play08:22

call this

play08:22

to do list and i'm going to use the

play08:26

i'm going to press ctrl command space

play08:28

bar

play08:29

to open up the emojis and i'm going to

play08:32

type in a

play08:32

memo and then there's a cool little

play08:35

emoji icon that i'm going to use

play08:37

that looks like we're writing something

play08:39

so just a nice little ui there

play08:42

and then all of the items in our list

play08:44

are not of course going to say hi

play08:46

but they're going to have a little check

play08:47

mark whether or not they are completed

play08:50

and then a title for what the actual

play08:52

list item was so let's start building

play08:54

that out

play08:55

i'm going to create an h stack here

play08:57

instead of this text h stack

play09:00

open the brackets the first thing we're

play09:02

going to add is an image

play09:04

and we're going to use a system icon so

play09:05

we'll do system name

play09:07

and let's do check mark dot circle

play09:12

go ahead and click resume on the canvas

play09:14

just to make sure we are all connected

play09:16

still

play09:17

we have a nice check mark there we're

play09:18

going to customize it a little bit later

play09:20

and next to the image i want to add some

play09:23

text so let's do text

play09:25

and we're going to say this is the first

play09:28

item

play09:30

i'm going to change this later of course

play09:32

and just to make sure this is all pushed

play09:34

to the left side

play09:35

although it already is because we're in

play09:37

the list but let's just double check by

play09:38

adding a spacer on the right side to

play09:40

push everything to the left

play09:43

now we're going to have a whole bunch of

play09:44

rows in our app and each of the rows are

play09:46

going to have maybe a different image

play09:48

whether or not it is

play09:49

checked or not and they're all going to

play09:50

have different titles of course

play09:52

so because we're going to reuse this a

play09:53

bunch of times with different

play09:55

information

play09:56

i'm going to make this its own component

play09:58

this h stack so i'm going to hold the

play10:00

command button

play10:01

click on the h stack and i'm going to

play10:03

extract

play10:04

sub view and while this is highlighted

play10:08

i'm going to call it list row view

play10:12

so now we have a list row view in our

play10:14

list and the list row view

play10:16

which is created down here is just that

play10:19

row

play10:21

so before we move on i'm going to

play10:22

actually take this list row view and

play10:23

make it its own

play10:24

file just to keep the code kind of clean

play10:27

and separate between our files

play10:29

so i'm going to right click on the

play10:30

navigator create a new file

play10:33

i'm going to make a swift ui view i'm

play10:35

just going to call it

play10:36

list row view which we just called it

play10:40

click create and i'm going to go

play10:43

back into where we were with this list

play10:45

row view i'm going to

play10:47

cut the extracted sub view

play10:50

i'm going to jump into our new file and

play10:52

just paste it over

play10:53

the template hello world so now we have

play10:56

our list row view here and if i click

play10:58

resume on the canvas

play11:00

of the list row view we should see just

play11:02

one list row view

play11:05

so let's customize this a little bit by

play11:06

making the text the title here dynamic

play11:09

so we'll create a variable at the top

play11:11

we'll say let title of type

play11:13

string and then we're going to put that

play11:15

title into the text here so we'll just

play11:17

put title

play11:20

now every time we call list review the

play11:22

initializer is going to ask us for a

play11:23

title

play11:24

so down here let's just put this is the

play11:27

first

play11:27

title and back on the list view we're

play11:31

also going to get an

play11:32

error because the list row view is

play11:35

looking for that title so now i can

play11:37

change this

play11:38

and i can put this is the first

play11:42

title here if i click try again we

play11:45

should now see it

play11:46

on our preview

play11:49

and because we're going to have a whole

play11:50

bunch of items in this list i'm going to

play11:53

reference a

play11:54

data array instead of just typing these

play11:56

in directly

play11:57

so above the list let's create an at

play11:59

state var

play12:00

items of type and let's just make it an

play12:03

array of

play12:04

string for now because we don't have the

play12:06

models completed

play12:07

and we'll set this equal to and the

play12:09

first item will be

play12:10

this is the first title comma

play12:14

this is the second comma

play12:18

third and in our list we can now loop on

play12:22

this items array so

play12:24

i'm going to delete this we'll type 4

play12:27

each open the parentheses we're going to

play12:29

go down to the id and content completion

play12:31

because we don't have ids on this array

play12:33

of strings

play12:34

so we'll hit enter there the data will

play12:37

be our items array

play12:38

the id we're going to do backslash dot

play12:41

self to give each of these just a unique

play12:43

id

play12:44

the content we're going to hit enter get

play12:46

rid of this ugliness

play12:47

and just say item and then for the code

play12:51

we'll just add a list

play12:53

row view open the parentheses and the

play12:56

title of course will be the item

play12:59

if i click resume on the canvas we

play13:00

should now see

play13:02

three different items on our list so

play13:05

this is

play13:05

looking better and before we get into

play13:08

the actual

play13:09

data here let's go ahead and create the

play13:12

navigation bar

play13:13

items as well as the next view so

play13:15

underneath the navigation title i'm

play13:17

going to call dot

play13:18

navigation bar items

play13:21

and we're going to use a leading and a

play13:23

trailing so we have one on the top left

play13:24

and one on the top right

play13:26

i'm going to put these on separate lines

play13:28

just to keep this organized

play13:30

and the leading is going to be an edit

play13:34

button which is a default button that

play13:36

comes in xcode

play13:37

we're not going to actually do anything

play13:39

with this button yet but we're just

play13:40

going to leave it there for now

play13:42

and this is going to be a navigation

play13:44

link so if we click it we can navigate

play13:46

in our navigation view

play13:48

so i'm going to put this on its own line

play13:50

i'm going to add

play13:51

navigation link open the parentheses

play13:54

we're going to use the title and

play13:56

destination completion here so the title

play14:00

is just going to say add and the

play14:02

destination we haven't made yet so let's

play14:04

just add a text

play14:05

saying destination

play14:10

if we click resume on the canvas we

play14:12

should now see

play14:13

these buttons at least on our screen so

play14:16

we have our edit button on the top left

play14:17

which is this edit button

play14:19

and our add button on the top right now

play14:22

when we add these navigation bar items

play14:24

you can see that the list kind of

play14:25

restyled by itself

play14:27

and it will do that based on the

play14:30

environment so based on what buttons you

play14:32

have

play14:32

but i don't like the way this list looks

play14:34

so let's reformat that so

play14:36

on the list i'm just going to add dot

play14:39

list

play14:39

style and we're going to start typing

play14:42

list style to see all the completions

play14:44

that we have

play14:45

i'm just going to use a plain list style

play14:47

for now

play14:48

open and close the parentheses and our

play14:50

list should default back to how it

play14:51

looked at the beginning

play14:53

which i think looks a little bit better

play14:56

when we click this add button right now

play14:58

if we click play on the live

play15:00

preview it's just gonna segue with a

play15:02

navigation link

play15:03

to a destination screen that's because

play15:06

we put destination as

play15:07

the destination so let's now build out

play15:11

quickly what this screen is going to

play15:12

look like

play15:13

and that's going to be a new view so i'm

play15:16

going to right click on the navigator in

play15:18

our views

play15:19

folder create a new file this will be a

play15:22

swift ui view

play15:25

and i'm going to call this add view

play15:28

click create once you're inside click

play15:31

resume on the canvas

play15:33

of course and this is going to be a

play15:36

very simple view but we do know that

play15:39

we're going to be in a navigation

play15:40

view when we get here so let's embed our

play15:43

preview in a navigation

play15:45

view open the brackets add the ad view

play15:48

inside and now

play15:51

the content of this ad view let's make

play15:53

it a scroll view

play15:56

open the brackets and just to get rid of

play15:58

the errors let's just add a text

play16:00

saying hi for now and on the bottom of

play16:03

the scroll view let's add

play16:04

dot navigation title and let's make the

play16:07

title

play16:08

add an item then i'm going to do the

play16:11

control command space bar

play16:13

to pull up our emoji keyboard and i'm

play16:16

going to type in maybe

play16:16

pen and use one of these pens this

play16:20

ballpoint pen

play16:21

because i think the emojis look really

play16:23

cool

play16:24

and in our scroll view we're gonna have

play16:26

two very simple components we have a

play16:27

text field so they can type in what the

play16:29

item

play16:29

title is and then a button to save or

play16:32

submit the

play16:33

the item so let's start with the text

play16:36

field we're going to add a text field

play16:39

open the parentheses we're going to use

play16:41

the title and

play16:42

text completion here the title which is

play16:45

the placeholder let's put

play16:47

type something here dot dot and for the

play16:50

text we need to bind it to a string so

play16:52

let's add a variable to top here

play16:54

at state var let's call it text

play16:57

field text of type string and we'll set

play17:00

it equal to a blank string for now

play17:03

we're going to bind it with the money

play17:04

sign text field text

play17:07

and if we click try again or resume on

play17:10

the canvas

play17:11

it should now show us a text field

play17:14

it's not going to look very good by

play17:15

default so we're going to have to do a

play17:16

little bit of formatting

play17:20

so here it is type something here and

play17:22

let's format it quickly

play17:24

let's start by giving it a background

play17:26

i'm gonna use a

play17:27

color open the parentheses and type in

play17:29

color

play17:30

literal i'm going to double click on the

play17:32

color literal and

play17:34

go click on the other and i'm going to

play17:36

go to the

play17:37

crayon box here and maybe use

play17:40

one of these this mercury color is

play17:42

probably pretty nice

play17:44

and close that out uh before we add the

play17:47

background let's make it a little taller

play17:48

so let's do dot

play17:49

frame with a height of 55

play17:53

after the background let's give it a

play17:55

corner radius of 10

play17:58

and i want to embed it a little bit from

play18:00

the left side so the

play18:01

text is a little indented so before

play18:04

we add the background on the frame let's

play18:06

just add dot padding

play18:08

dot horizontal that should push it in a

play18:10

little bit

play18:11

let's hold the command button click on

play18:13

the text field and let's embed it in a

play18:15

v stack quickly and then let's give the

play18:19

entire v stack some padding so we'll do

play18:21

dot padding

play18:22

and then maybe 16

play18:26

maybe a little less 14 so it looks like

play18:28

it kind of lines up with the navigation

play18:30

title

play18:31

that looks good and underneath the text

play18:33

field let's add a save button

play18:35

so this will be a button

play18:38

open the parentheses we're going to use

play18:40

the action and label for the action i'm

play18:42

going to hit enter make this two lines

play18:44

but i'm going to leave it blank for now

play18:47

for the label the text is just going to

play18:49

say

play18:50

save let's make it dot uppercased

play18:53

after the quote here so that it's all

play18:56

uppercased

play18:57

i'm going to give it a foreground color

play18:59

of white

play19:00

so that it has white text let's give it

play19:03

a

play19:04

dot background color of color dot we're

play19:07

going to use the

play19:07

accent color which is default in xcode

play19:10

should be that blue that we keep seeing

play19:12

let's set the frame on this we'll do dot

play19:14

frame with a

play19:15

height of 55 so that it matches the

play19:18

height of our text field

play19:20

let's give it a frame with a max width

play19:23

of infinity so it's as big as possible

play19:27

after the background let's give it a

play19:28

corner radius of 10 so that also matches

play19:31

our text field

play19:32

and let's change the font so right after

play19:34

we call foreground color i'll call dot

play19:36

font

play19:37

and maybe dot headline

play19:41

that looks pretty good to me the button

play19:43

looks good if we click resume we can

play19:45

click on the button

play19:47

obviously nothing is going to happen yet

play19:48

we haven't added an action

play19:50

but this screen is looking pretty good

play19:53

so the final thing in this video to wrap

play19:54

up let's jump back into the

play19:56

list view and where we have our

play19:58

destination and we click that add button

play20:00

let's change out this text

play20:02

for add view open and close the

play20:05

parentheses

play20:06

and if i click resume on our canvas here

play20:09

we now have our list set up where we

play20:10

have all of our list items and we can

play20:12

click the add button

play20:13

and very nicely we go to our add item

play20:16

view

play20:17

we have our back button because it

play20:19

because it comes by default in our

play20:20

navigation view

play20:22

and we can go back to our list so this

play20:24

looks

play20:25

really cool that's it for this video

play20:27

guys we set up most of the views in our

play20:29

app and in the next video we're going to

play20:31

start looking at the

play20:32

models so that we can use more than just

play20:34

these plain text we can actually use

play20:37

to do list items hope you guys enjoyed

play20:39

this video as always i'm nick

play20:41

this is swiftly thinking and i will see

play20:43

you in the next

play20:46

video

play20:50

[Music]

play20:52

you

Rate This

5.0 / 5 (0 votes)

相关标签
Swift UIMVVM待办事项应用开发XcodeiOS应用编程教程UI设计Swift编程移动开发