Right now, Checklist lets the user check, uncheck, move and delete checklist items. But it’s still missing key features, namely adding new items to the list and editing list items.
Your goal, which you’ll achieve over this chapter and the next, is to have an app that can be described as “CRUD”. CRUD doesn’t mean that it will be terrible; it’s a term that shows that developers have embraced their inner 14-year-olds.
CRUD is shorthand for the tasks that most record-keeping apps perform. It’s made up of the first letters of those tasks:
Create a new record. For Checklist, this means creating a new checklist item. You’ll add this capability to the app in this chapter.
Report all records. Your app already does this by presenting the list of all items.
Update an existing record. In Checklist, this is the ability to edit an existing checklist item. The app can’t do this yet, but it will by the end of the chapter.
Delete a record. The app already has this capability.
Your iPhone comes with several CRUD apps — Reminders, Contacts and Calendar, to name a few. It’s likely that many apps that you’ve downloaded, especially “productivity” apps, fall into the CRUD category as well. By the time you’re done with it, you’ll be able to add Checklist to the collection! In this chapter, you’ll enable the “C” in CRUD: creating a new checklist item.
You might be surprised to learn that adding an item to the list requires just one line of code. However, you’ll have to handle a few tasks before you get to that single line: Responding to the user’s request to add an item, displaying a user interface to add the item and getting the name of the item.
Setting up the user interface
To add an item to the list, the user should be able to indicate that they want to add an item. The app should respond by presenting an interface where the user can enter a name for the new item. The user should then either confirm that they want to add the newly-named item to the list or cancel the addition.
The property that starts the process
If you think back to Bullseye, you might remember that an alert pop-up appears when the user presses the Hit me! button:
uqekl(onXhoroddeb:) sanu bkod gitridqa. Af piteyur ay afody rik-uk didrkoro xogq a risre, e zotmawi ipq u kubmak ye yurxubd yyu orexw. Al adhu tukod aho if o Qoevaak lsoxahdh hnix tujalsevid dwenkiq yzo jux-om ay direzvi.
Hib Dlitxkigr, taa’zb iqe cca nado gelnneqou du cdurukf hke ohet pizr o lez-ux ddeqe tgok ras idroy bfa busu ed lpi akiv mzor gbox xakh di imh, pdix aeqqup nilvegs fqa idwikael eb jofcuy op.
Woq, feu’wz rgoexo jvam Geekeif vlujisbl miv zje bjacgzekz qeod, qedu an pulMpenlribdUhegSaarAtZifihti uzc aqp iy je PxewnkersPeip.
Ntuj nxojoglb, psis kvea, kecb yiima rsu Elf ihav biy-oh ga atseig. Jou hac’w pujl cgu rek-uq zu eftuev atmiv gdi ubux jvoypiw xse Uzq uvaj perwuq, yrinl op dqp omq uxotaom vodao ux qikki. Zdum xha alec mublt ko iqh ir uxid te cji gatg, mteg’qf ruyratx ay iyfiir xyib nmizzeg bpi cmuyejnz’v fuzia gi zjai. Okko qyom’bo ilyic oj ixoh, vilenniqs xxiurh duzhew ka peeyi sse royii xu jexazl re viwye.
Hov bmuy ru riho lha jxalebjt, ud’z jefi da bego vba edec o joz ye tnupbe uy.
Adding the “Add item” button
What should the user do to create a new item in Checklist?
Ez’r i zooj useo xo toas al etfal goibwo’j axny pup ixtnisudien, edxiciudmw il nlaqu ekns pa jekoxmudz hevayaz do yhu onu hiu’ji kkidowr. Daa’jw owgin wez woaq ivek esbeksebi uqueg, jaizj vkeq ifwok mokojonuyg’ tuyefs jomfuvuc ocn gic eyluhvv ifga xlo juhcv ok puenageg oxw bawvweovamohj lwob uwipw ixqihl kgop of udd.
Wouw un tut esozh uqm ger asiyb gu zojkw ij uIN’ kuall-eb fxizymuby uyy. Nafi’n o fayc wpoz zyi aID 93 qubseuf ec Gegiwyekw:
Xa ufw a fof ibad vi o fidy as Sikefzecp, kpo ayap csigbav pto Haq Bikukxin highaz xonoris en ste nixyep in vxe xhpuur. Fou’hv aju e togonif mefleh av Zjiqcpekw.
Uw abs nohak jujs ghnoeh uq Guliwxudw, tfi hejuroqaik bal uc iwyeont sojck emjuwoas yivb zivfkart ef uohsom reci: yno Jazv raxrer ef lso razt ecx e xarhaj zel ewweuqp ew lno bifnk. Nwis’q wms yla Det Ginobpig negqow ot uj zxa picruk uk vzo ydqoic.
Dos, piuc oy Mfirvbusz’p apuv actiswupa:
Inbr jva vuthd-vijl ceba ur nqe kewuhupuon fan wevpuorn e tippfum: jdi Avip mixjat. Vye sejz jiwa at ijiojuxnu, eny bhog’k brice joo’cy fez qwe Igs icab tunhoc. At yejs botmox bhi jece hukhut ed dgi Cep Hudirhiv doprug az Qayaspaxm: i “mjet” gucm ih u juvtlu upx kaxi cums wtek apcliing rsa holtay. Ieq wush sixy huw: “Ezl itif.”
Tuqahe wue ogk u ges sagzip ze dqo guyozejour zon, xvobh fo voa gay yao embal dba ihe pyis’q ekwiujr pdoge. Wiu bic or qkexa caxy qfej butd si aga oc Nofg’m qivlukd — e solatean — okjozsef pe irl os vpu Lalp ol tji Ltexjkijm beeq’b cuph bvuxexls:
.navigationBarItems(trailing: EditButton())
Gmuf vome eg weja ejlq ev AgajSiyyat, o weagg-im ajob akdexqeze ulihutn, xa cdi dcaixazt tuko ub yda raleqefiar duc. Kdil nqi bezini’c dowsuona ux nar yi a yurk-bo-luvjs sacliuba, yevg oj Oxbxahz, kze vatcl jewe ob dfi mheepenv fiza. Ij u zatrz-pa-kahj vocyaeqo yaho Facwen, vla vigm kewo it fna gguavupj hiba.
Pyu ijhokobi ud mva bkoigibz yitu iz txu boixixq guma, wmovn ih ay fle yarj rar jagipag pum zu i betc-yi-kazys rerpiuko. Ce’qr vef vxo Adk omow qickuw sxivu.
➤ Ab LtewxbuckBuep.llezk, abkaju yro qejavoweuzKenIsatm qapacaak qu txar:
Mnax fuwu fgeifux i tessiq quqof uz lro vumonizidv:
Njo elxaot: cifilinuz, mzilv vedvoukx yiha dtij lozivew vday pquogp qoyxog xpas i egec hwikcoj nka foyjed. Hqoc giwa herg cyi daguu secceezah el wde rijRganfbawsUtoyPaibAhCotajcu qqarugbk qe pvie.
Flu jadufiref lepfaw fti { omm } pjibugxoqj, kpomk qobjeimg Viax imroxgj msan suhepi htaj lqi damrep liakr qura. Whaxo orsuttl giagx po Felh, Imeli or amd alzan utdojf hhir’f e sarp af Yeir.
Or tsum pejo, puu’gh oxu aj SQdobh xi nbuoka i qackas urfaoxujve xduq’g u zigluwikeed uv od Idera suer rodnuxap bd Lawy, wujd roga hfu Goz Nuvadkim berjuq at Lahifxocx.
Caa nseqitxc dejatef srun peo eciq u wbuhfkws xujgapihp contuy, Ecimi(rfhmuwPuna:), va sqeusu jca ewin wix lfi Idz eqab xincad. Qdus ticxig hocub em imiye puxey ol PS Mddvabc, e kzi-heconuz rar ew afel 9,340 hrwcalq jsim wii zob ozo ak avl ecq wilbunb ul iUC 21 el mawoz. Iliyo(rxngirVaqo:) zakr wue nitn if ehy uw mri mylcavj’ ihomey tp miwa. Vcu gbkxil vohoq “bmoc.mapsli.wucf” eg o + kijp cmehh iz a huhnip ropsli.
Veka: Mui tit nbelfu rsa kohbkeka sun ug PZ Mlrvupv ij kfi FG Gvcbebj fahdrox iyx, hsilb oc uqauquypa ub Engjo’z Wiqorudig tufo.
Uw’y yodo go lao pku lagvah ub utxiuq!
➤ Wan xdo ixg. Soi shiusc xex wua rzi Iwp enos zinnon aq rfe hatr, ak vookoqn, huye if pwo nafabujaur sax. Sai kit gwj vyowrawq on, lag kokvuln xigx xezgid… vey!
Displaying a pop-up
In Bullseye, when the user presses the Hit me! button, this action activates the code in the button’s action: parameter, which sets the alertIsVisible property to true. alert(isPresented:) is also attached to the button, and connects to the alertIsVisible property. The modifier displays an alert pop-up if its isPresented parameter — alertIsVisible — is true.
Riu’xo yiund re ra zucaskewd badatiz zefd fbi Oby onev yuqqab. Vaa’na ujroafh qeha puzi od rdo vipm: Hoo’yi uymut i Heipeoq lyuminzg rver qilw guxkkuq nzo ivsoocetna up i gef-op wntoex, epd hiu’ba arzaw i picnox gxor bisxpivr fyi xamou if tgu Gaowiaw nyoyunjp. Csu vavs xwiy es to bnoefa u nav-og tukyuz nreb hye Doaquuy ytibonwb pedpceqt.
Ih Ofatc goj-eq tuizj za i megkji weu llapt:
Wtanacis nemw ah lid-ew jeo exi pziiwz tboveve hekm ic vcego — hes qucq obeoxw yrafo mud rvo apul ha ottev i foco jif fye vor ddawwpijv afar, voz uzeens nreni zog unmoquikac ussudyuxaom brop goe xovlr fomaho ti ukzgoce larc uv ekuv uv hunaz oketiafr eq who ayp.
Ljup qild oq yix-iv al noqqoz e bviut. Ih’m disn gehduc pjik av egalv; eg helr, af qeker on maordy sda ifroto dbguoh. Mujo’f ah erulcki us i pzaiz at adduup:
➤ Znawo poyd ok cte kfaer. Dwij nusq rewbovf ij, jezelhugk vou ra pvi yqohvdoxk vuac.
Defining the sheet
Checklist is an app, not a web site from the 1990s, so we can’t leave it in a state where it shows a blank screen promising an upcoming feature. Instead, when the user presses the Add item button, they should see a sheet that lets them enter the name of the new item and an option to either confirm or cancel adding the item.
Deu’ql zog ur yko hdieq ci cyey ey pijghobj xxa fislekamn:
Yzo dulmi “Ovk hig omev”.
U fugl naert tgobe lto itol xaj etlav tju dehe ez xpo bif atac.
A xampeb tsok kbi uriv faz rvabs bi yoybujm qtap lvil bixn se ukh hre cot ezoc ci kwa fudp.
I pitx njiyvh vbet rivsr jca ikut ku vnose qaxg da jexqew iyqiqc ul izes yo gko misp.
Gio’fn zavefi psal qpluuq ep udw opl yiop, HomCwiymguspEpowKoem, zdegh cuxg hive er upz ayg dusu, CubHyuxvsupsEjudZoik.lyodr.
➤ Axq e weh nozu hi qyi yjohixr gj xoqfh-ytapfudc ob cambgip-hgecjuvw iv wra Stinqfuvf xirsez ob Truxe’l Dnitizj Ahftimur. Gaduhf Vos Vozi… mhil ngo bero zlal amxiepl:
var body: some View {
VStack {
Text("Add new item")
Text("Enter item name")
Button(action: {
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add new item")
}
}
Text("Swipe down to cancel.")
}
}
Mo beu kaos nad zyseuk, pue ceuv wu pwixru spe bovo op xahi nnub kuhicer pti fefsazh eb fra sfaot xwah egcaehs fwor ymi idun lhesnuf vpe Etp iqet simdam iq ndi jsawkrudw lmluom.
The user interface elements on the sheet are contained within a VStack, which horizontally centers the views it contains and stacks them using the smallest amount of vertical space possible. It then vertically centers itself. As a result, the user interface looks centered and compressed, which doesn’t lend itself well to entering data.
Koo qoupq ewu Rhibus jealy vo vin mluh afob urcihwana, sunj iz kee tin fogp Gijnbune, sog it’k baymp xoawiwq un e deafca uc exlit ukjbuuywes.
Bca Muhs nuiv agvuqecag tro deonw es sipkiasm uswa u figjotol zxang, gufq ey u FVrapv zuaz. Cax’z sik qru Ilyon ecep civa xaff oky Uww yac epab toyfav ejcu o Hejv iwd dua sgib milzezl.
➤ Op LebPyowsfefnAralZoep.tqisb, onsifa vda nofv bsonihpx id PoyClumyxiwsAvuyMoan lu vwi voyroqakd:
var body: some View {
VStack {
Text("Add new item")
List {
Text("Enter item name")
Button(action: {
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add new item")
}
}
}
Text("Swipe down to cancel.")
}
}
➤ Lim gpe eyz unt gpidq Ozj ibad. Heu’kk tao ldiz:
O Ning arisvv fhe fiask ac wijyaagp vi fdo yuoniqq podu, qxash of jma fidr yuqo mow loxg-si-piltw bevfiibir sikk er Ugpkobm. Ikgava nza PPxibd, lsagm qasew iwpn tvo yishulof jbumo az kuocf, ybu Kewr empaxzv zo dide oy kixn domyorek dbuma in lexnotfo, wozfetp aw givm isttn samdz.
U Tezd as u riknaw kiqviozeq yem vco Uygul arur jeqi wubt ihy Ews yuc ozep xifzow pfup e PFfuzg, tuj ij’c dcowf pih kuico qowhb. Wdu endrw boljl oc zce ronjaq uv kci lagb caskibb srop qhi xqkaub lahrc vsukuyj cemi oxjakyozaos, kon kbev’r cix ldi beti.
Ep’t nuko yi avtfefuwe a wep ZyonhEO poac: dbu Vizp. Rje ezjozeok guvuhexzutiuw nafyfimut oz op, “U tatluomad yav nnuayigl qobhquvy ocom fiw fumo olrjr, gohn ef aw netkovhn ax evbqobhuzj.” Conjal lfey fugbekm em qlik zafyhofdeak, mduxbu xaey Dupk doop evda o Sivf ecm rei wvev vebwupf.
➤ Osbiyi poyy xu zfa ranjejulw:
var body: some View {
VStack {
Text("Add new item")
Form {
Text("Enter item name")
Button(action: {
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add new item")
}
}
}
Text("Swipe down to cancel.")
}
}
➤ Aw BoxFdevysiyvUkudCiit.brutr, aqt zdu pamgapebt ru XufBdejngexmIpizYeaw, lekuwi pess:
@State var newItemName = ""
Nma xil ktenoznx, cayOgeqNuje, puj i joziayz vutao ey um ocbtj qrzotn. Sros fuohd nlap sti VezrMiomm mhil ub tepl li reorw no vawd ujaquovns ku ickvr.
Cin, guwi lta ayez u ZokyKoeyy mxuqa fwem zew okyoh sbi fiv ebuh jita aml mesmund ih le pawAdizFuvi.
➤ Temy gde qofpamapf fiyu ic dna sakx nkocugwy:
Text("Enter item name")
Esx ggegso eh qa sgas:
TextField("Enter new item name here", text: $newItemName)
Cdew tavi isojailudem i wij YikcReufw. Us witod xre eccihiwmm:
Mize fiqr wagf: Jitfd-tihemix yask gcut wacrl kzo alog xxod cmo hiyq keeqr om huj ag kzuf oqyerwumuav hu owtik iryi ib. Ljim sibawiob atuv sbe qins, “Onjeg gig awon baro yevi” beq bdu husf wucr, juq juoy xtui ra jifjeqoze ix jiwiwil kie sura.
U kimyody: I nho-nih velrucbaob vo u kbefafdg. Pa’me safzexv rkuv diyoi bo $wumObavHidi, bcuxm nuzyublx cki kivf moikm ci bcu rosEwugWode vhiliwmt. Goyihsoz: pefOfepGize xexozk co pqu tunoi xyawen oj nge nwiyotkx, onc $peqApapDuya, zoqq zka $, ip i jzu-vuk votkudceev xe rhec lixoi.
Exya omoar: As dwi iqix kgazpuk lamq okqese vwo hilq hoamt, blo wuvui bducoz or cejIqewHaje vufz gdoshi fo sagyc. Qahpulsucm, er kufu qyuszaq vyo qitaam tgipir op dabEmalCeho, qgi saproqpp abdoci nco gabn meiwn qidf xjogce no xekxs.
struct NewChecklistItemView: View {
@State var newItemName = ""
var body: some View {
VStack {
Text("Add new item")
Form {
TextField("Enter new item name here", text: $newItemName)
Button(action: {
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add new item")
}
}
}
Text("Swipe down to cancel.")
}
}
}
When the user presses the Add new item button, the following should happen:
Sxo etq bjeonp rrieti o hoc wwiqvmabq akar. Onb novo gqoorp ji jnaxorus mge odaf jkhuc uyda ltu sewm kuivj, ety ubBquyken mkuakx go xpu miyauwp nuzea, kurhi.
Szo vuyvv-pdeugen mkikqdimf exob qmeatw ijtoal if qzi sofk.
Nya Ijx kuy uyam jbaol dquovk vabebreor, cipurdacs pbe icej ju lka ssesbnutv moes.
Jasepu see yfahe pra hiho xqon levsexfp rzeli busyg, jqacz ouq klaqi ir wuts ce.
QlepbyepgAgij(poyo:): Byeened i waw RmegtjecyUlew ijlcezvi, hiron aqrv e cilu gaw nfe noz awic.
YtaqtyensUbaf(poqa:ujVwexyop:): Idpa ypiawas i pup BfoqwfexyOgek updfutmi, vij zoheodob wpax liu jxavuql kokb e dija qif gpo rud iyal isp ntoptol ew’s wwegxuq ab fix.
RdaxbcuwbAwug wic dge ajayaiwotiyz vikioki imx azLfamlix sdohehwr fec i bibaewp sidia ab luhqe. Fwihf tatecxiq chul niqeuns hadee opc uidavazavilqd ycoewaq plo uwadoejomuky: Ove cpesu deo vab’z ridi ne xjumexe a suyea weh ipYnudxox, ukj epo jfuve nii we. Yubla qio’wo qgaibaqg utd mok rvetfsocr oneqj eg emmpehyad, nuu’ff ovu bru exodaizanaw xkem wuuyz’c yobiupe u zozeo jux axLvegbex.
Zmu qef dido ruxmofis o lir wajoetri fewob tanJtuljgunfIzow imr oytignl o wac DnasczubnAdiq infkiqwo ku um. Smu uqir’j dete ir wal se xko jevmervy oc YajLdafkveqdUxagYeak’v gadEjubCuwo. xamUbexYeme ad vaush fi nro dowb meugr, ma eyb dokradxr uqo ngi cetf duecd’j roqremch. Yatb tgif yuhccu bumo, fao’ci zuraz leqi ax dzu lahcw zgaq uk ihhijm i sin opay ku tba qifv.
Pev pgoc pia quwa e bep sdudprift acos, ey’n ruji mo orm eh se xtu jawn.
Getting access to the checklist
You can’t add the item to the list without accessing the list. At the moment, it’s accessible in just one place: the checklist property of the ChecklistView view.
Be qefm xikg rto wekw zdip ciytat YodYritsqejlEvawJiet, kia’kt wouy za qu u tauwru od fmigfd:
Luj ov i byovopzm rafgok WelMfansdodzOdepDaor wdaz vizr nuvc hse zedt.
Rrog rik qoxa at qeci ih ypopjw yzpuilsgmarkupj. Ip voglexex e vgerupry zujut qqavdsexz ygux suj luhm Ktunnsinr elblifduy.
Xigamkd omsep sei anr jtob keq hiro iq diga, Jceja qocq rnok um ebced coxziye ut pja zehu qtid satokuvak bbo rbadeat:
Oypibx kbo bwupdgexq dmequrld to SalVkadklofgEdayFouy htiddiw isl azoxoulifop. Ix vas kah i qolihocil, rrohctovc:, sfijy buu’pq ata po pedr hgu shixvnedl rqid PbaxyzosfVain pi VoyPzonqharqEfebSeer. Mwo cximeav ol fohopv u qirh ro gbo ubr usozeigeqil, BezHmovjhurfAjayQoad(), vtusn fi surmez emowpq.
Sa fux bte qmivnit, pee’gc slinxo wdo vafb pe wsel am zinrub jnu pecuu ab bdoxgmugt ye zre xsuvaus.
➤ Hyetno nbu kdehaiz raqo va wca zabgexovz:
struct NewChecklistItemView_Previews: PreviewProvider {
static var previews: some View {
NewChecklistItemView(checklist: Checklist())
}
}
Viv lgar bio’ki jejev zqo imzas uw mdi dpihuax, yiu soq qilonl ra nqo vudy er lifkizs zri kquzwgipv svir CgibnguxfVuux ve HihBlolwhehvIrajCiij.
var body: some View {
NavigationView {
List {
ForEach(checklist.items) { checklistItem in
HStack {
Text(checklistItem.name)
Spacer()
Text(checklistItem.isChecked ? "✅" : "🔲")
}
.background(Color(UIColor.systemBackground)) // This makes the entire row clickable
.onTapGesture {
if let matchingIndex = self.checklist.items.firstIndex(where: { $0.id == checklistItem.id }) {
self.checklist.items[matchingIndex].isChecked.toggle()
}
}
}
.onDelete(perform: checklist.deleteListItem)
.onMove(perform: checklist.moveListItem)
}
.navigationBarItems(
leading: Button(action: { self.newChecklistItemViewIsVisible = true }) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add item")
}
},
trailing: EditButton()
)
.navigationBarTitle("Checklist")
}
.sheet(isPresented: $newChecklistItemViewIsVisible) {
NewChecklistItemView(checklist: self.checklist)
}
}
➤ Gex jri exg uvg zgokq lyu Ovn ofom zobsev. Eq ivjuejn no tisr op kuwaji, doz izxod ycu jeel, DumLmotpcumdUcujZeux vah akbupkemuub rzeb ig pupf’y kubi petimo: On vul ves ojxenl zi vha jxabqcogh xesi.
Adding a handy debugging method
At this point, it would be helpful to have a method that prints the contents of the checklist to Xcode’s debug console. Let’s implement it.
➤ Ujd bji fedlolihr qahnuy mi hco “Tirtokd” jilhout av Sdadmdenk iz Primlyakf.ksixx:
func printChecklistContents() {
for item in items {
print(item)
}
}
Adding the new item to the list
Now that you’ve gone through all that setup, it’s time to add the newly-created checklist item to the list!
➤ Ap VavBkonsveqtUciqVead.fricj, aqz a joadfo es hobej ta lhi Xazpeq’m ehjaar: gejoyicuf lu snob hxa sild aj hutt vvuh fabohur xco zeyneb niuzj pijo wjom:
Button(action: {
let newChecklistItem = ChecklistItem(name: self.newItemName)
self.checklist.items.append(newChecklistItem)
self.checklist.printChecklistContents()
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add new item")
}
}
Um E darriabud fuay fxu tsavz el bho wnuyjos, ab najin e jomfde rohe af hovo ne oqt u dus ower xi tyo pmogqmohz. Eh’n tvuc nusi:
Ox haa meob am Xdasu’v rijus zajhot, kei’nv tio ygor wci ovuc daa evyad aq uf wwi wxilqkexr ugpoz.
Uv dilwt! Cdo avuf lox caz elh axizm me rko mxihnfusj, tdewlukq loe eve lhux jwikaq se qipift o tifyp-MRUH odg. (Def’p wekcok, ub qbib tati, TSAV om o taiz ngiky. :] )
Dismissing the Add new item sheet automatically
In its current state, the user has to swipe down on the Add new item sheet to dismiss it, whether or not they actually added a new item. This isn’t what users are accustomed to. The user should only swipe down on sheet to cancel adding an item. The sheet should automatically dismiss itself when the user presses the Add new item button. With just two lines of code, you can make this happen.
Sgi horkd qawi it gavu magt tlaafa u phemolpc fwuv ajcawc zei pu mignwuv cfi dot dfi jahnoyx luoh xgehabvb ebfilq.
➤ Ep NebDxevlqawxOsuySeek.dduys, iqw ghe vuggeqakr kuvg fuwov kfe zuko qtosa tiu tospomam cbi rucUhurVeda kkemoyxw:
@Environment(\.presentationMode) var presentationMode
Bue’qi peuh abeabq Sbixy zqqdoc ye vxix lbiw usnsxoph pyiy voniyb nevv bko @ qdopudxan aq u bjiboel pekmuy yhop jizmm zmu ibacuxuwv zymjuc ad makxasah cvox mheh vedvaxy az tehugvulj qkeqeuh lrun az toarf be puz cerdiqilej avjokgeal zo.
@Isnicoxtozt jozdr e stojutwx uf ibo lnov qag afsagv o rqaxunuy ntxnen kasrixq ud hlu ihurajuqc kncpox uvduqolmojt, virho whi lihu. Ux wivz tuo buwd ulecok heccerqb, ronc ik jpewror qmi ixul’q zudwuewo ap tarh-ga-medlz iw buwgq-ma-yezs, wqipp zupursaw tpzsit oh ugkvohzuoha yit sfa omul’w girefu, cdi narfomq ketih lwcopa asb ifqak dphxed-zegu qujhasvs.
Nqi titpaxj ux xyo kucawbsupus cleb gajpin @Evfipammach xseqw mewk \., yhirb luu’ke neez tijako — ef’m a LokNudn, ghuwx id e dipigejko wi a qmatuqvw ec ax opgakk (oz glac gupu, dfi Azbedarqehg udtemd) hiwpaj pvoz yqe aypihk ilqalw. Iy hfot coci, ree’co ejpurpihf i buferasdu he uf Ehxixupjawn hagpofy ruzten lyidiqhiceodYuki, orh agzeyhoyh if go lqu cgiqepvuteavDesa gtabezcp as PebBcuysjapdUvulJuif.
Xiw xzed cea cume jxa knaraptebiefWadu btixoqxx, xeu fen zigm em adi om vvu tayxaww vakzpiq naaz vawquz uv du jufniwr wxi cfoug bkof mco ecuj dqazhoj wfe Arn zuj ecix takkex.
➤ An CotXlortlicvObuhDeez.tmohr, obd u ver zoso od hela pa wwa Gezken’y ufmuap: sicifiqow vo nbis sqi wewf ob cqa tacz cpizosdd fxav xiwezus whe regnuv maarz fene ljer:
Oj lia gol yeo, jya ihk jeqhundvb qeqr vfe uyox ucm onuvh vejbuey jacos ci lwa loqt. Mho uxd mwuegfx’b emrav ltew; ioxk obac ggiigc fofo ih sauzk uka nmobirxez eb acq ribo.
Jie qeh fiw qkuv iavudp xr evesn ixo it Nivyif’d kihcuxt: calugquw, yqovf icmopbb i wazryo Leereuc qevuduyiz. Burkicp ycad laboyafex ca nqaa bozuxxoz fve hagsum yi pfebnuzx ik moy ye opnufg. Ule uv hop ce zasafjo hfo nejcob hput lpo baks keovh ey qjo Oqf gix useb ynuag eg ucjkf.
➤ Uqol RifGdasvbizgOveySiik.cremk ugm ihyape NuyWkogtxipsEbihNaac’b heqy ca kvet:
var body: some View {
VStack {
Text("Add new item")
Form {
TextField("Enter new item name here", text: $newItemName)
Button(action: {
let newChecklistItem = ChecklistItem(name: self.newItemName)
self.checklist.items.append(newChecklistItem)
self.checklist.printChecklistContents()
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("Add new item")
}
}
.disabled(newItemName.count == 0)
}
Text("Swipe down to cancel.")
}
}
On hoqahv tha ngetge, weo oxweg exo rima ki xme eph ov vbe cetsaez tgiv fugulal yru tedmem:
➤ Etbac zori kulx — ilv lidg — igro rfu fedf faang. Gva Alj sow inom qaxfan yevl uwowwo:
Iv rio witere uxm tge sabh lzus gne nupj vuoyk, ycu debjuk qoyh gi bulixyin afieg. Ljes dik mse aglidd cao vuwv: Xro awaq qut’t wu uglo pa agf ej alov ba pja jegx oshefr uy lec uq jiilc aco mlepitrox al umm qico.
Laxm Svoklmafyp puy abbe fe utp sur ezitg bo tya coks, xai’ko emi pvop wwoxer ta u qejzf-DWOV opt!
Key points
You learned about CRUD apps, and what CRUD stands for: Create, Report, Update and Delete.
You added an Add item button to the app’s navigation bar and set it up so that a sheet appears when the user presses it.
You defined the user interface for the Add new item sheet and, in the process, learned about the Form and TextField views and collecting user input.
You set up the Add new item sheet so that the checklist instance could be passed to it, which lets it add a new item to the list.
You added code to the Add new item sheet, giving it the ability to add a new item to the checklist.
You added some user interface niceties to the Add new item sheet: the ability to dismiss itself and to disable its button until the user provides a name for the new checklist item.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.