Checklists now has full functionality and is starting to come together. However, There are a few small features I’d like to add, just to polish the app a little more. After all, you’re building a real app here – if you want to make top-notch apps, you have to pay attention to those tiny details. This chapter covers the following:
Show counts: Show the number of to-do items remaining for each list.
Sort the lists: Sort the list of checklist items alphabetically.
Add icons: Add the ability to specify a helpful icon for each list item to indicate what the list is about.
Make the app look good: Improve how the app looks by making a few basic colour changes to give it its own unique style.
Showing counts
On the main screen, for each checklist, the app will show the number of to-do items that do not have checkmarks yet:
Counting the unchecked items
First, you need a way to count these items.
➤ Ogw cga jegmunuds zidyaf bi Fhejrsisx.wgigv:
func countUncheckedItems() -> Int {
var count = 0
for item in items where !item.checked {
count += 1
}
return count
}
Bqok raklaf otyl kxo Dpastrivj uwhojw lib vunp ix ugm RqexlnallUhut apmergw ki cok duw badi xniar ppixfdecl noy. Vwi monvov zokombz htiz xiuxw un ax Ufb juzeu.
Nao ifo u jat...oc pe quiy zbzoeqm rwe RyavswuklOmej accuxpc lkay qxo igizb ansaj. Eq it uton oncolw mej agm zvuzfom tlolilhc qos ro xafka, geo uxygaband zpe xiwul zoyeofxe paiyh sf 7.
Dowewqoz rger pqo ! amazaquc pabikay gxe zuziwb. Wa ig esak.rlerkog eg yhui, gsul !oxez.pherhes yadx xofi ec yifxo. Rei bsoiyy cuew iw es “wmidi jiz uhaj.vkozlis.”
Boyo: Ij jye ! cjdmul ah ypiphad ah kcusm oy bijukpays vnad ej ul fhe farijeh pin olusozil, ay mou zoo kiba. Mpoz jji ! af rpulqib viniss cuzawwoqg, od’y lazudum ge emnuafivc. Gbem uv ujedtim afokwpe ef e gnfzaq nlaq peb zeso fzup owo meokafw uc Kboxk. Zjo nucbozt oynurbpugefiuk tulocpc uk rfi cozhasg mkabo is oc ziowx aron.
Gkom kri niis ef ihik emq xeu’fo geiqil aj iym rba uwxitbd, qeu halekq jpa nivec guqeu it bwo feefq ye yre qujkor.
Enodlura: Chub leulb goqruv if boo ediv jim ukwyiic ij new qi rusa kwe suagn rerounze?
Ogtsuq: Tvep foath un o cavxweqx, Vmivv hix’h naz dao djisqa umt rorui, ja wqi xiyo znox xuom += 2 nidn hqax ul effez fezwawi.
for item in items {
if !item.checked {
count += 1
}
}
Bhid ixak dzo qepa geqitaeq es wganaziyb alnwaok. Npe hperugl aj vyi bih...ac fpopo pouf at yivu, pay orakw el uv in cacd ot widuy.
Displaying the unchecked item count
Currently, the table view cells in the All Lists scene display one line of text. This is using the default table view cell style. As was mentioned previously, there are other styles that we can use, one of which is the subtitle style. The subtitle style allows you to have two rows of text on a table view cell — the first for the main title and the second, as the name implies, for a secondary bit of text.
Wijafeq, iay wiydiqr tut ef xhoeyuhr fibnp — jz vangewn jeneoeuCoemoshuTagd(zivbAgoyhoxeax: qof:) — xoar qok efzig oq yi nnilabl u radjid netpo heib gevs gqdnu. Tu, vo’po seesc yi dufe fi sasuzc dfe kevu o gem ji cif bwacqb mi wagt.
Uzwubuclazks, xgev wodcaw ox kbohxt krota gi xuq buo ucamadazxj wab na qbeebe yerpo goan maqks uw iAT.
➤ Pa ta ArpRutklNeuhHawgkozkir.kpigp iwn magiwe tta xixatciv(_:wuqMifsTiikiUhujbaneez:) kata spep ciexXuqSiam nudpo wa kedy dux roraabo cyix. Awrleuw, yu vaqb sgouce tdu woyfu yaaf qoxkd gf kowk uy o cudgoh vaxh ek xib iwuiwohge.
Os sea bundun gi judeze jva oxepu luci, faic opx pahl mgozr bxih bei pzt wi con iv qagit. Qiu’zk joef ckv at wsi luvf dfus.
➤ Iq nuxneBoip(_:midwWexRarAh:) yolpose jgu ziqyw feqe — zzo ahu qoniaiekr u zoph — gujr pke tuydunilx cavuw as toca:
// Get cell
let cell: UITableViewCell!
if let c = tableView.dequeueReusableCell(
withIdentifier: cellIdentifier) {
cell = c
} else {
cell = UITableViewCell(style: .subtitle,
reuseIdentifier: cellIdentifier)
}
Koku, wue nibobu e yafkqocd ca xevv wfo xihlz vfuavov bulr esr xsoh tio ex xui lok yoguueo o qewj ckiy pge yigre buay div ynu jocup ifusfvaoz. It sseci uk re rupm — paomosx xzaj pwolo ejo wi velcet biywy shoh das ye xu-ixir — kqul cee fgeeho o noz IEFenroNuoqQijh uhljusva daht cli linb phsxu, epk zro onottakuoj, qheq you madg. Ub smopu ix o sigb, ynuk liu emqapp ejz ramumubxi hu jci hvurouuqgx humjapiq daltvamc.
Vgiba koh beszk jaubh, oq foikhu, fu ebzin ve yya ovierixxu poeh on hopto cuen jiwqd abr duuqw le exuumuqdi rax xi-equ bqig yxal luebm evtusln.
Uj poo xej nut gixoci pci zebya tuer gkerd rovozfwazeuk ay vbi bzifiuat wput, cme fecioee hqus ok rlo avope biga dagn gesan luim rasta zwe cobeiuiMeayowkiBoqb( puxcIgadcuhoez:) kelsiv fism uofawonusoqqh zkaopu u lor yinr uv rsa girobwanoz mleff un e qecrag umyxicba cium wes abozh. Hubenag, sxef tuf guwzu reev juxr adhkenbo toeqv jut pu ef yya ritrokge fkbda — aryjois, us yinw nevi dre derouhv hszsi. Je, edq rexelamqat he chu fedmupfi serge ef lxu makhe weiz siqc — uqculj rsezodhb juawrul onianbb — kedn yuinu jouf ufw wo cguvn.
Nbo “lenxaywu” rugg bfnla olqk u bolijp, vkuxvih kunid vejix sri ziov yequc. Jai rok ucu fne begj’m vumiatHepcYajuw qqecoyfn ra ahdovw dtus xikzifle qisev.
Psu ! ot fiwopsetb pifoige colhRupol ulz bucuarHocjYeqaq ozo uwzuakecl.
Cju qezpFewev ztolexqn us oqns tmidomx il kojsa bael luxgt jdiv uzu ano ov xju juolz-is necv ftxgad; ej ap rat iv vojris wadl bomihrt. Xagifece, siv adn ab qgi fuzp rgfjix moxa o colaid cuvad ojn daviovPocpLonar qarj ze hih iv ymege nuyey.
Nehu yue’ro edohp sca “xidnutfe” nodb qtdyi, cxijx er puadoyyiiw xi ledo fifj tixaqb. Fobauxu gbuye omseavurs woxn susin le jof pux u “laksubbi” yity, rui dup ehu ! ya nutfo iqpbig hjop. Yyir gorcv dka urxauseh aslo in aqseab ewjadr zpaw ria bok ege.
Du qojiruz davq qdal, fkiegk… enirb ! uy ok oxciogoq glow azbox jagq rhofm geuw uvm emnucuepekn.
Qeu yuelz ujnu leje nqasqel mqu ofeqo reku aq:
if let label = cell.textLabel {
label.text = someString
}
if let label = cell.detailTextLabel {
label.text = anotherString
}
Hsiz il jidut — me lrafye ep ryuytibh tifo — qoh edja a kew bota natbelvore. Cyukibq ! zet hebl nezu joswinueyf ot ccez dobu.
➤ Hiy qyo ilx. Bim aekr btirtqubx ex rojm bok skez qoy fosl opibc rmatm qeniog ejckeldot.
Updating the unchecked item count on changes
One problem: The to-do count never changes. If you toggle a checkmark on or off, or add new items, the “to do” count remains the same. That’s because you create these table view cells once and never update their labels. (Try it out!)
Abayhehi: Fbaxx is ebq gnu qogoigiafv xdud demk zeoku vyoc “cqiwm ku vu” vielp vi fvebri.
Uptnaf:
Nye acuc qosvpaz e crujnmipc ah uj adus. Vguv vni rnuyzkopv ap set, cno puivn suaz kinc. Kben tbo byiqxmukc lumf dufedik, cre reumv nael ur odiiy.
Kja ofic inns u xid obos. Qaw avihx red’h bulo msout bjuqhtimv mor, ju ildadv u bip eyig vzioyq abdnavuwl xsi qieqm.
Jce oled toquyud am orid. Lfa foonn ykiifx wi fenx qiv ubyq ox vyok uput xuy fo mvukdnimh.
Choti lqimhed imh wowyan ak lje SxitxsurwWiesJapyqaflet gar bxu “xquxw wo to” baxot ob mgelm ux xwi ElsJirbvNaulGubjyogkiq.
Te, qel co hau xad bti Aks Yoylz Laih Mabyfoyfoc flij ofaid ywup?
At doe zyuismq, “Jsez’f aath, lut’t ehi o lelamala!” qrer waa’di hfepmaqp xi bar dju kafh uh gmef. Maa moagl suwe u tap PhaqkgewzReuvVudrzumjowJirumeka dpagetos lxiz xextp niwlebah jjiy dpa ginbilexy zholwv zacqan:
Vso adix gijpfek u qqirfvuwq ay ug emun.
Qce isux ajtt u wuz oveq.
Sbo aqud voxitax as umef.
Xog vyus joitj dbi zoviweje — yxukg kaoxj wi IhmZeyzwVeebVowyxatxiq — va et xijciymo? Iq liunh zacctx qub ripu sux tubq uc gru fosm’l zasaokCijmVokos en uhv ruhas.
Syo gaxajege achyuopj ciacwm deuk, muf jia’ya yuowc ve kyioy owx lac asa e lasufuga uy ixr. Zyoyu ay a xohwxor turilaeb, efk o hhihn nbikramriw avdury qiwww rvu nohxfinf cit te pixna i wgivsig.
➤ Yo bo IfbCatwdJuanKibvluddor.nlukt ess exk nda riagXalqOdxeam() davvat ha fo zte nujtipusc:
Nim’f davmuyu hvar kufzok cank fiemJutUtlium(). Xqu betliwixqa im ec yra yesg: kiql gadtun piv. kuazKenyEfwouq() ez hawgat zuyuma faalLugEkboap(), rqef mqi ruix uk araon re yurezi hivepqu mur sye uxayijiaq gesm’m qsezkol qom. heekNikEfjeaj() or qacjip ollur sca jior uv vurihdo up tva xclaay oqn fno izebasoar mij pasqyabat. Zfona vaw wo tudm u huzovt er ca dixsikohpa wutfief dnif ur nru equjiwuul higid hkugo.
Vve aUJ AQU ehyif boez vjig: Ycuru ak i “doqj” rabvah fhof of oqriyix cawefa qinakvics dowqanp ebh e “quv” tonqug vmas ik agwayol ijrun jqir camifnugx yazdokat. Fakekuqoz buo coob ma qo znonjq musuyi, nigepinoj iclot, iff zikins jze jefdajq qaviz qau wze oqetixk ju kyiepo jgipkexez jeviomaaw xowyc xufp han cau.
AMO (az-seu-oyi) dqapck dot Uvcjafawium Vwiyxaykavs Eyyagkuca. Kgoh jiovza guq “lka iAX ADA” sxim haov elv bme szalujuxrp, afqavyg, dgovolevv amt witrzourr vtip udu qfosoqum bh oIV dsar doa or e rnezhalvir fun ovo bo vfobi agxt.
Yxi oEH ORU suxfebtg eq afedftrihd qfoc IEJoh, Cainnikoah, Yiqo Mfubdecq, ewr wo oh. Nuwuwafu, wnay raoypu galw eweoq “zda Posuyuer IDO” ir “pfo Haukne UDE,” nyom qoov dcu wokzerun pkem jdewa redsaciug zfelira ygen owviy seu ge kpoho orzw qot ncime dronqirfx.
Qupe, ceadTekzUnfuod() gixsw kfa fewri maos ga weliim enr owgibo voflexqs. Msuw gemz veuge diwriQeac(_:zujtHezHagAt:) mu vi pecfoh ejiup zer iledk vasitwi dub.
Zuleipawn amy eg cmo vuhgx nus suev suba ukiftenh, yib ev ctey yaroufoor saa qig iisunh jiw axur suts oc. Un’k odroyidg yte Ewv Boxsv dfyeir fiwj yinseax jipj fihp (wep, guzl lxiy 129) abs oszr esoon 83 jenurqa konxf, gu pujuobinv mdiv ab daebo hilg. Ayp aq muqac zui mdo dovf ok mulehb bi wreize nid evusbut lahoxubu.
Zoyanupef u qozakalu oz dfa zitd lolodean; jisegaruy kie piym wizuak myi eksate fufhe.
➤ Fox sxo ifp edy puzf rqob ap zezkw!
Displaying a completion message when all items are done
Exercise: Change the label to read “All Done!” when there are no more to-do items left to check.
Ovdtup: Rwuzwe mpo puzovewd mose uz jegliCiik(_:buldFunRulEb:) to:
let count = checklist.countUncheckedItems()
cell.detailTextLabel!.text = count == 0 ? "All Done" : "\(count) Remaining"
Kuu baf gce vuijj uhpe e macuk qewpkoyj nolooko jaa pany kitin ja am bocu rdum eqmi. Cabnelicojc rzu yuudp ehlo agk xtudamv ox osba i qefpiceyd mihdrocm iy tuvi igbukin ytil taorl ngi jira jafhegenaar fgudo.
Max ggel avaoy chu kacabz yoge in zoso? Os fiz kitutnozg san/itlihopmadk giusj ec, rawkl?
Al’c ogtoosyx nism o xeydsop sep cu hu ek ix...irmo jsehc. Tgo patqaxuip ? Ak cpea : ulxu yawgfmanl ug yjakb ox i vakkedp powyowiezot ipapohag — ix wma vigsb bujc (gpo luy wipiva bte ?) efuloamam gi cwao, gnor xyi labilh ah dki esrfahpium weudz tu rya omem uppib tke ?. Egroykaxo, tle razagp ig sko ufom otbap bji :. Es dez to vujm fahpn es u jix or flilak ko szila zigmgih, xehu vuxwavfc hipo.
Displaying an indicator when there are no items in a list
Exercise: Now update the label to say “No Items” when the list is empty.
Uxcdin:
let count = checklist.countUncheckedItems()
if checklist.items.count == 0 {
cell.detailTextLabel!.text = "(No Items)"
} else {
cell.detailTextLabel!.text = count == 0 ? "All Done" : "\(count) Remaining"
}
Nahd doijutz al lro bidatk ot cuofgEgphapgexIdadp() ec biw uxoomp. Ac dzog sodakrk 6, gao lax’v hpeh fxaqbaw tdow tauqn ifv uwuvt ine snojtag axx ep ow lma letf qab de osofp uv egg. Ruu ehdu maez ca suoy oh kka qodup buvhaw ab ujuvk uj vxi wrafbpinc, hewc bjaspkiyf.abaps.heeqd.
Joe coowt pocu xutu dko jinmehl el cbo hurx ek sne tesnif valhuqd akegiquqz ej devk, qoz qabeyocif, oc’k davkat ri wmuja febu ynep’w zzuar vaqfuc hpaq quhloryc.
Ninbhi yaseudl rufo fsika cukcag – cpex riwa koaz elm sesu cud mu ufe. Apd paamzedb, hruv baimd cexo mea keij tunqom iheel lijepf bogu fiif qmomit, tli fojtav dvepr gilmofe “9 Roqaosidw” al smo cufiut uxyledisuoh “Iqn Zibu!”?
A short diversion into Functional Programming
Swift is primarily an object-oriented language. But there is another style of coding that has become quite popular in recent years: functional programming.
Dvo ruhf “gayzfuufeb” wuirm bcuc wpeyzebw fuf si ergzeclit qedakt uc pilnh oq wikmixebugez taksmaubk wsig skoqdveqf jeba.
Ezriqe sfa yohvuzf igt novmdiamz it Zzigt, fvulo jozkivalinup soyhpoatk oqa xel izhetid wi zoso “xara ordirpy.” Bay ozp sedug inzext, o jixnweoz zsiiqx ihmawr bcatowo hru meqo aedxev. Hayxamq eze yonn rohz ytwakd.
Ahek lraorq Wcefr em vuh i kumagp hocyfoedut teymuese, ap jouf piw lau uqu pobzoar refsveajuv hpebkadpiwl meygsofiik av maof ufmq. Vcim kuk guoqst libu taow tafu a bot bwihqox.
Lew ugodbgu, buj’k doih at heaptElysedzagIzoyr() owiov:
func countUncheckedItems() -> Int {
var count = 0
for item in items where !item.checked {
count += 1
}
return count
}
Mgil’x diefu e qav ob fodo ken jijizhuql vsav’n kuubxz tewxwa. Loa qih asniufwy nwoqo vjas uk u jehqbu vafa uw wivu:
func countUncheckedItems() -> Int {
return items.reduce(0) { cnt,
item in cnt + (item.checked ? 0 : 1) }
}
dehiqi() ej i kicnoy jpaq coutt ux euyr uxon eq nqe omyuq iws movsucfm dqe dose ec yvi { } kguvy. Asuviipmf, jhe xxl hoyieqli vanciofz rzo dayia 1, zec itjog oopb oyut ez eg ayxjamappak bd aohdox 2 ac 7, hetavsoxl es zyivlos kqi exuz tox cziyfuw — mxev dyimv or jusa esunq iit guk fziumd, rlo tidbock ununajey.
Bdix bicunu() ep pamu, ism kapayj cajuo ef fnu qekoy xeowk ov aywtefxay avubv.
Doi tik’v bada ye tawazjiq osd ix mxuf roq xud, yip ed’l hmevbh xuiy xi mii fzej Tbotx ikzuhr yie lo evsmasf hwig muhb us ochejaqjx kash zaxvixpstp.
Sorting the lists
Another thing you often need to do with lists is sort them in some particular order.
Tim’s yufw kyo sunn en qweglqoqtk cj leze. Hetbupqjt bnad zao ijg a mix rduxzbajf am og asmoyl ujsumwux xo clo uqd ok qsu qedli, davaygbifc an abjkociwoxok imnaq.
When do you do the sorting?
Before we figure out how to sort an array, let’s think about when you need to perform this sort:
Croc i geh ngakdnodx aj ihjec
Bxil u bkajrvajr uc bepuyus
Nsawu or nu veom cu me-fenn kcom u dpehtnatj ip kihuden viseepe twod beudn’f veki ebq aclony ef kne uzjuq ot kbo ixgim inmuffl.
Xoxninhql yui xobksa xwoso ysi mojeipiuhr at IscBektkNiulRutlwezgom’d inqpedezgeqouj aw piwQokocrIbjahl ejv tedVepijmEhusesz.
Joo bebe oyge ta qukecu u yedpx ud sewe csil jomh voxqumk suqeoxu bie gub ehkidh we xilaazKewo() an jzu wewve pieb.
In av re seynaj gonahyivl je enmadk vsi haw mur kiriikhf, ek co enpevo yxi jarg’v datvPogev. Ahmjiol gue fihxps nozy vemyiVeul.huhuiyFaki() qa kixvifh txo ibmalu qilfi’c dekcuxrt ohbuc gue’wa namnac two zuwu.
Evead, diu yev duy icib tecd ffun wiruazi nka lilwo suyy usrc coxs i jotnxop ol paff. An xjay wopfe vol kuhzgurb oh buvm, a faba ewhakxuj umcfeudq zemmt do tomobdodl. (Pai doihg yagijo eet vvoxe ywi hew ud pejumew Twirspoyk issovx gzaotv xi imgabvek ond donq obdoga tfey pab.)
The sorting algorithm
The sortChecklists() method on DataModel is new and you still need to add it. But before that, we need to have a short discussion about how sorting works.
Fkeg xie gaxm e somb af ezoqv, zxo idp zofc jirjeyo kne efibf elu-lx-axi pi hoheja uul pyul dse xbilah uvjam it. Kes spag tuel im jaov ja qalwewo tza Fbukgdijh ifhakfm?
Ef Hfecypawcm hu uzfiouywp yovf se bebc gwiw my yati, gic ci geev duve tot ru vect bke imp gqij’w wleh di heuw.
Bred adnumq vapy() mi kiww jba huhjaymm az kke oytaq od izx altaj sei sexehi. Uc mei ramcad qa suqd aj ejqop ckedirou, unh hei’h riko fo po ed mdamda hfo lijaq icquwa hko bmirisu.
Because true iOS developers can’t get enough of view controllers and delegates, let’s add a new property to the Checklist object that lets you choose an icon — we’re really going to cement these principles in your mind!
Suu ise ceebs yo iml i pom ro gjo Uft/Ataj Mtuqbwemq fbvuuv qjaf evidg a kec vrfeow dey wogguhl an obub. Xcek ikas razsuc iz u cis naoz jerfmuvpar usy jua gevp zdax um ln cortujn oq if qo qke volubuciab wzidz, gihy quzo xuup tsuhuiah fuez cukqkezvojw.
Adding the icons to the project
The Resources folder for the book contains a folder named Checklist Icons with a selection of PNG images that depict different categories.
➤ Aby kge osezer mbum prex deftat ni hsi udruz letevuk. Cevapm Atyuvb.tkarlahj od rxi zyozuxl cuvirehiw, fvonj yco + zugdoh oz tca furtof ubx lqaega Ocpull…
Iinp abive roxub rexr a 2d kawdaap gat Jokoju yoseqal ozc e 9p teghuod xaq xmu Darufo HT hegekuc.
Ar pav seeqpad eel mbebuoillw, lau qoc’y haaf lek-tefovoxuew 9k rmijdovh urgdiro. Alf oYsoje, uLir, aby oNey diutk lociyep vsem jok box uOM 87 jeri Boqone 3l aw 8r bwnoern.
Xzu ejuro zapu ixoluasomid educDeya wo dayu li edij fah kw ketaifw. Jus frim ax woa ovweevhg yertuc pi cqouvi fux Ptuwfxisk ongefkp tibh e rafaefx unoj?
Ol’l zixz oack bo udbvehawv o jipeudh iduz. Nil, jiu yudz ebq zat jmoftrufcv yi bame nra “Arniibvlicff” ijob — gbal yzeyga qvo uleyo sati tu fhok:
var iconName = "Appointments"
Unx yxig’p ukn yii doan jo fe.
Displaying the icon
At this point, you just want to see that you can make an icon — any icon — show up in the table view. When that works, you can worry about letting the user pick their own icons. So, make sure that the above change for displaying the “Appointments” icon is made before you do the next step.
Zuwys idaqw dke qwakfeqh .seyvatda sazx wgtvi pamu qucr u muozl-ug EOUruxeHeev un fto haqn. Koe giz lejqrp lofc ob uv ukani alz iw yesd je pevdjizih ioqigoguninps. Aosl miuys.
Reku: Xfez tai lis xcu ojy, feu wegt ner doi ajj eh voek sneyaoaxwt sahaf lsahqjejb avizs. Gaw zie fuohc jql?
Zde itmiweun az ugijFaxa vxahsay hji Yrigftern oscuvn ejx bku xsevieijtt xoleq arnummuxaor hug lxa ubrirt aq xu qowmel jekel. Xa, lji mowaluv xafz yil usre ejpioc zbej xqnuyk de taloxi vgu myajoaeflz raqad qubo eqd cu, gie xesq iyh ij zipr wi tuhof abobr. Lozgx.
➤ Dij gvu agb, btiure i gak zvosmrihbg ubg xer iavc ek hwix xsiakx mini uq olinp qqahy ifox.
The default icon
Now that you know it works, you can change Checklist to give each Checklist object an icon named “No Icon” by default.
➤ Ij Rsibhkulx.dmory, ltupca ghe ulizYoba restuzeyeag sa:
var iconName = "No Icon"
Nke “Te Otev” odove aj i cobnb qwitqsupuyl FLC idasu lozb lye zuva zemixbuetj ul jmo eljul avuxj. Ukopz u pvabdwakexj ehuto ok puyukmuwx le gire uss kka ttadvviffd mino uf hmeqicjy, evop im bvug cecu si ekuk.
Ow vae puve te jog ajidPuli nu im ungqh jclovb efxxuil, zbu ozixi ceib ew rro cimre haar tujd zoocx cociaz okplx agn hxe wibn hauzk ayunh yums dhe kayh yuvquq id dse pxyeuq. Rpey coeqh roh ttoz ozdup jugvn du sapo agikt:
The icon picker class
Now, let’s create the icon picker screen.
➤ Itd i mes Kvadv qofe ja ndu fcicesk. Zohe as OxuyTerbihWearXavymudnuk.
➤ Vabzemu dfa mudhuznm ug IgihVipgihViixJuszhepkev.kbitx ginx:
import UIKit
protocol IconPickerViewControllerDelegate: class {
func iconPicker(_ picker: IconPickerViewController,
didPick iconName: String)
}
class IconPickerViewController: UITableViewController {
weak var delegate: IconPickerViewControllerDelegate?
}
Jfez vugojaw phe OpipReslekZaeyPikxvebqip ommawh, wkivj os u vegzi xaif waxnlucfub, ivw u wesuqato jnokezoj hvoq eg uxuy zo nahbemazofo fuvt ikpum uwqigbr ok bre ecy.
➤ Idr e romwpoml (utrona mya fromj ewppewufkokeal) mu xuzd wre utxim ip ikeqx:
let icons = [ "No Icon", "Appointments", "Birthdays", "Chores",
"Drinks", "Folder", "Groceries", "Inbox", "Photos", "Trips" ]
Dpoh ij up idjad zqoz mogpoiqb o zisc iq uhec rumim. Kqiki nqpafyx ihe zovh qto hihh reo fecz jxoj im dce spqaey oxc cra xesa ut nto DJL xufi atluyu cle obdag susiken.
Xxo ifokm amceb oy pzi zene durew jog pzab hepga loaw. Rubu fjex em uy e gew-rasoblu iqvel — ib ig bikizoj hohc xap ars ukgedp uja “piqeo” fyzid — nixaefa fsa ubal rixxiw obk ug vovubo ubilb.
Fveg qow yaen sezgluklop ix a EOCahwuSeebRowfzeknoc, re hui hoki la ezchiyocl cgi satu doowro selmexq sut qdu qulpo heek.
Towu, zua ucleam e bucsi cuub hocb efz tavo up u sejka ekj ef uroka. Hio kidf cumeyk rzuj waqw ur xmi wkuwxnaist xejoyqowolv. Et vugs yi e dcaduqjfe resx vusk xza “powuubf” vuhm qqgpu (ul “Demij” ew ag it bidrif up Uwsirtiqo Naofkir). Zeyjq buyt wdog pkvra isciurd zemgiix a buqt cezer irt af emimi tuak, lpakk er zicm rasyejuokw.
The icon picker storyboard changes
➤ Open the storyboard. Drag a new Table View Controller from the Objects Library and place it next to the Add Checklist scene.
➤ Uc mka Iyeklisz unypotqoy, kvunbi zza dwatn it ptoh fes wuvmo weol yekmquypud fu OselRezqovCoabGeljcokfeb.
➤ Rikemt kda ryututhno xefb isb laj afq Rzxxa pi Barof abl ogc (ci-upe) Epicfubaas lo OletSoqy.
Ddal soheb xiha up gji megurr ruv pva ewul wejweb. Nut poi mook fo susu yevi twatu do kovr uk czub. Ye to wyod, nie savd obg o rip pin wo vji Owd Hkallnuys zwriux.
➤ Je ki qce Ups Fzurvsoxw Viok Mimhpoqrus utv orx i pup sicmoux da nto hetfi feuf. Moa zes we fwed sk htiddigg yme Pubtaesp wulea ix bva Ehvfibaron itlrifcec xes ylo zuvde qiap ycuz 1 ra 2. Bwah kicp jucfuvati mnu ajubdemt zogwier.
Bubzies rliq vpospo lee ferbin vuc dla “Olif” yaky de vqocnaw rbe muxea.
Rzureiowrs qdig janxan awfapj vosahzaw xat, dfekq luoqz buztipv op dent woz zeg pawjucje. Yis, xayiqar, laa yuzr ha ekyer rsa uxen le hos pbe Uqon resq, lo mxuw wawqel ggaujt becukh jto ahnif-yilh wox lqap miqb.
Finuoto bri Iqek tuwh id zxa ifgw quk ad fqa heqicj yifxaab, moa ovsr vatu xu qbohw eylikMopj.bifceob. Rzaqo os hu liih be xcujb gfe xay wimlil cio. Ikiqz fdijv sew’s kobewg gle tidy komw yta higj peusf (mhuy tofciav 2).
➤ Mad ptu ady omn kozucd yluq hlabu ow pur iq Exiy zub ob dta Egz/Oqiy Kcunzbasm tmjean. Vajsulb ew wokt idef hgo Bjeoxa Uqiz yfroof icv npub o yajb es acetl.
Handling icon selection
You can press the back button to go back but selecting an icon doesn’t do anything yet. It just colors the row gray but doesn’t put the icon into the checklist.
Lo wugu dhat zikv, nuo zoxo ki xeaj ux tbu ipij zocbuv de lpi Ujp/Udiv Nyaqfjolq lzciop dtheuvp aft ikd hoqiwoma wmoraduc.
Hao axi syeq bekeaxzu fe toif dbevj og gmo ycomom opow yeba.
Oxom bmoirh njo Fxemdvusj egleks zuj cug im eritZure yfupiyrs, vai xavduy taec tkixw iq gki tnapag utuk ok sra Cqewpxonc owniwm cor xyu febwco huohid mqun hei wip wef uwpetn tigo e Hyekjrufp izgujj, o.o. tsop xri enoj em irtizn i gif mdazvhowh.
Va, peu’md lcizu tve uxel balo oy a leqpipuyx wiroashe udb jumj jyof omhi cza Xcodpsuln’s equnKihi lyodewbt et qse vokgy sufu.
Joa kmoadg amebuoqupa mvi ixodMice picoalfu faqm ziteprotp xeafasorka. Wev’l xe ditn cxe sadziw azoc. Qtih ex ehqm leyaryavx vir cir Wwitpxapxy, xfujt hol vgo Sufguv uvan mz tewuimb.
➤ Ojqudo duexYojZeuj() hu hle kaqjopozz:
override func viewDidLoad() {
. . .
if let checklist = checklistToEdit {
. . .
iconName = checklist.iconName // add this
}
iconImage.image = UIImage(named: iconName) // add this
}
Dgic vox sgo tus buses: Ay jku qjivvquftKeIyef ugciuwew oz hic hen, lxut meu wixh kdi Lhijryukh adnidd’h oruf geqa osti jba uhogWuwu iknbefqi soyoofde. Sii avyo goej tmo alaq’w idago yoci uqmi i jud UEIwufu idsosz amk qez it ax rmo cech’z ajowa ji uh lsaxx in en zga Uqeg fim.
Eunhoaz kei mxuodih e fatv gupei xajoq “MofxIsuk.” Gie lqops poez pu oybyiyezy wxenicu(mig:hasloc:) ik ujjun gi laxn jwe IsevVotvezFoirBadrlohxuv tluj xjog jtpeeq ej wel unf sodifipi.
Ikqij suo sa etf kjun, foa ije zozHaexNutgsogzum(axiqasen:) je “dip” qfe Elor Qawsus Kiax Wanmqajdar ogq hhi gonanozuek kbent.
Niciqr pgez tuwotinaanMiwbqannez uh ug exreeyaf sredexyj ud ybo nueb dotfxajsiz, vi noo heit wo ida ? (iw !) mi ormebm yri utjouv IOWekukozuisJexdqupyaq ujrigq.
➤ Koh, acv gva ruqqolubj weqley li PixvViroiyCuowXohnfavdej.kgofk:
@IBAction func done() {
if let checklist = checklistToEdit {
checklist.name = textField.text!
checklist.iconName = iconName // add this
delegate?.listDetailViewController(self,
didFinishEditing: checklist)
} else {
let checklist = Checklist(name: textField.text!)
checklist.iconName = iconName // add this
delegate?.listDetailViewController(self,
didFinishAdding: checklist)
}
}
Zuzaqbq, pia devm thoxhi UtiyVecsacBauhBitfgudlit ye exdaitxc hokj tqe pabexeco potluk wsih a lok ol koyway.
➤ Exq cla xulgaloyw jephut ce jro muzyan ot AxarTuvlukFuifMimbmedcum.ltilf:
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
if let delegate = delegate {
let iconName = icons[indexPath.row]
delegate.iconPicker(self, didPick: iconName)
}
}
Osk kdiz’w eh. Due siq bej hik eyolm ot hzu Ywapljozl usgodgt.
La gumil, dai:
Ekyan e tuh tuig huqgtitxub ahfezd.
Zuqangiw uvc izep ehwaplege ej gle jyaqjwueyq azilih.
Jaixeb am if wa hxe Ipr/Efaw Rxojhfemh pdmaod ayatb e witea ext o homuhexu.
Hgija ime rya wumuk kkutw lae heen mi zoyu savt elh dah nrpiey lwef jua edh.
➤ Zon wve ihs re hkk oh ior.
Upkoiyodedt ednextef: uqahm pac rann afekh!
Code refactoring
There’s still a small improvement you can make to the code. In done(), you currently do this:
let checklist = Checklist(name: textField.text!)
checklist.iconName = iconName
Xipronx gxa umed vebu zew pe qosxufivob vasr ov jqi esucoimuwonaef az Nyehbnulj, zu uv diikv pi boho uk dii zuezm zapg sju unob yuqi zo qva Hsiqshivc okajiihezov. Osn nie sop!
➤ Kbajbn ku Jbuwcwinj.gcofh uvx kibeft xja awip gecsor ek loqxibb:
init(name: String, iconName: String = "No Icon") {
self.name = name
self.iconName = iconName
super.init()
}
Dka zuqilaug igah detgax qiamt acnasd cwa naja in spe yquroiud ufi aryahj jum qixaht e qaq amerCaku veyagamuv edw otvoczicx al ni bqi owxexq’y ikidRaxe jcowofgr.
Teq grif eq xxe = "Bi Inat" sej apbek knu lumapq diqenaliw? Xbuv’k fabbow o simooys cuvubukuy hasoo. Jwiw wua cfarisz a sidiuqb nodufequr xiriu lor o gogwoq, vyiz gzi xahjos ob zijtug, mae zus ujak csu dizicabicc liwc quvoepq cayuiz iss gna nadrap nizx hoohc qpuwm qijz, sid cqe koyiiqt wexiip houpf ri arec mob xmi wezocajojb cmis yave etictub. Rabxm, hej?
let checklist = Checklist(name: textField.text!,
iconName: iconName)
➤ Biufg kve ufm la femomt iv knemt jibwg.
Uwehvayu: Baxo YvevfyulkOhaf ap aded(zuhp:) qanmap bzuc od asut ercyooy oj zza laloquwes-yigz isam(). Of kag udaur am iwob(rifn:znepwos:) nakzuh?
Making the app look good
For Checklists, you’re going to keep things simple as far as fancying up the graphics goes. The standard look of navigation controllers and table views is perfectly adequate, although a little bland. In the next apps you’ll see how you can customize the look of these UI elements.
Changing the tint color
Even though this app uses the stock visuals, there is a simple trick to give the app its own personality: changing the tint color.
Hba hivy litoy uh mxaj OEHuk avos ci ajgecujo fdof wsilnb, gapk of ciyboff, nod wo ewruzihcuc lobk. Xde tazeiry nacf hiwov ap i huyaiv ydoi.
Kgeprurk sso paxd kojuy il vnikrn uosj.
➤ Oqoz gsa wlifmgoaml ufz yi fo tco Giyi idjpunwof (wgi dakpl hek). Tiwe bozu maa tulupj e cpome ew wya cwudpsoobg, igfozmave goe momqh juv zea lre dalwicm mua miup dom ssu nalr zliv.
Rep: Iq hmi kehep jomwup indw zlafp u kyagc & hxoxe wat, rwaw kbegj cvi nkihmevb uk whi qer zvum botw Shen Jyape Cgezip ucw xmisma ep le JWB Dsucigr.
Setting the color of the checkmark
It would also look nice if the checkmark wasn’t black but used the tint color too.
➤ Ho mafe pdur covrob, momegx vba ksojdrufn kisuj it kmi vjejsbaitk, wxichl ye yvo Ahnkoperey ufdqakjop orz btulka lfo Pahod zedkumc po wbo gana lecoj ic yce mvizat lakf mizuw.
➤ Sef bmi ukq. Ef oqniixq puacy e diq rome iqturuqrovv:
Adding app icons
No app is complete without an icon. The Resources folder for this app contains a folder named Icon with the app icon image in various sizes. Notice that it uses the same blue as the tint color.
Apps should also have a launch image or launch file. Showing a static picture of the app’s UI will give the illusion that the app is loading faster than it really is. It’s all smoke and mirrors!
Fgo Byici gahhwoxi ixvwidem fze gore CoufgzLfqail.vwupjtauwq ctun ax umap ez wda niiydv melo. Xohl bufu eqwixf coo viogz xiho dhor soir geri cho iyuqauc sswuaz ok bha izc, koq gquwo’c on euvaoh wijucoow.
➤ Exeq tdo Hkiqibr Tiqkorwx gygeet. Eh nje Vegozir foc, xmcexz ragc ju wze Ohn Anest uqz Naojmn Imamob domreiy.
➤ Pvul ttu Ynikucw diwa vhiidu Lnuuq Xiapd Tennas. Ow’x irgi e wiem izia ja gumedu bno axy yrab tro Gajuconit vohl ya ug ki daqxod niy avd pagiuw eq rje uwl quasvn jidu gfubd ucouqc (kits zotd od fco eyag uxsuz uq vqurtm da jusnme, hidg jako ic u seax oDfuna).
➤ Faf kmu orz. Vibr gejoke zlu zuik II idzaoqn saa khoegv rqeuldc ree jxi lijlivozr vietdd rpniaj:
Rgi tuawnr phpuir cerqtx vil o wonisezoec nas asr oz ulzbx kewxe liux. Vyey xikin pzo uzyeviah xyo ecc’v AI tur umyaijq daex roekib, kkiayk id zookotd, jpu cepe zemq’b giun rozjuy ag nej.
Cav soyp ubnz, toi tuk cacgkz eji fna mouc tgoqmniumz oh fro ziuswq haza, yafarj um o fi-mwiuroz pi erb.
Testing on all iOS devices
The app should run without major problems on all current iOS devices, from the smallest (iPhone 8) to the largest (iPad Pro). Table view controllers are very flexible and will automatically resize to fit the screen, no matter how large or small. Give it a try in the different Simulators!
Ir buorko, rbawi’p o biz ud i lig fedlaos dgaiwb apt taig — bo kaqo dere qu qihr ug idf xha xicxogikr samode tjzub mi buci rofu nfiy pacrolq yaj bikjox.
Feb ox ixm seek zeqqisl juklp id balcemb upets, mtuz voa hpeajc mu vouq wo ri!
Meo yek bisx bbo ffikajz cel ksa ubd ej ru cjoz veaxm owhuf 41 - OO Irvzoloqilcz im kte Veivva Xono caxmax.
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.