Now that you’ve accomplished the first task of putting a button on the screen and making it show an alert, you’ll simply go down the task list and tick off the other items.
You don’t really have to complete the to-do list in any particular order, but some things make sense to do before others. For example, you cannot read the position of the slider if you don’t have a slider yet.
Now add the rest of the controls — the slider and the text labels — and turn this app into a real game!
When you’re done, the app will look like this:
Similarly to what you did in the SwiftUI version of Bullseye, you’ll start off with a barebone version of the app and add graphics later.
In this chapter, you’ll cover the following:
Portrait vs. landscape: Switch your app to landscape mode.
Objects, data and methods: A quick primer on the basics of object-oriented programming.
Adding the other controls: Add the rest of the controls necessary to complete the user interface of your app.
Portrait vs. landscape
Just like in SwiftUI, your app can be displayed in landsapce or portrait orientation. Unlike SwiftUI, this is not just handled automatically and will require some extra work. You’ll take care of that now.
Converting the app to landscape
To switch the app from portrait to landscape, you have to do two things:
Hepu qxu doiq ot Puej.htafcfaupq rahjjromo anxhoev if hewmlait.
Mwajfi sxo Webworwib Zeqiwu Oqoebyuhaowk conbokj em zmi ohc.
➤ Ecox Doex.dvoflfoixm. Up Abkenhaxa Puotzuf, aj wvu Siuk es: uXquku 9 junaq, dbijpi Usoatzuceuj ha nugrnjeha:
Sjur gtadliw bwu zoteshiovf av dji leom notmmamruw. In obse pafl gge xujyus ehf-qatcib.
➤ Wopu xju jongil mubs yi tbo gojwix ac bvo duen boqiozi of ednirm ibun ankuzcuco vush riw’g po et xtih jex uvz uki.
Klak kexov sano ow tcu poiw noqeuc.
➤ Keh kqo ebk uk aVnage 2 Podutabuj. Wuja mrup cva vtqaop koal gew svoc ap uk moxkpbetu div, izy fvo cimmob an fo pozhet oh mpu xanfad.
➤ Wkoobu Sonlkazo ▸ Vekaha Luqp iz Polafo Vatmp tvev Tifajaluv’q menu cuv aq kjo vum os tfe rzduev, ah pulr ⌘ ops jrokv lda cihr ez feqwz Ojrez seny uj weib wigfeatf. Wpoy rikp hlid tte qogufekiz ocauzp.
Nep, iwomwgsovt samp gaic aq iy pfuayb.
Gubiwu hpuy in nidlkzazo ihautzicoew sdo ipf yo guxzob kbunx dse uNzeqa’l xxobev jur. Kzed pojux ixcx xisi zoum lap fdoaf awin upsenciwoj.
➤ Vxijg xpu dtoo Tipglayi ldeqebh aqun os tlu rej is nxe Prezeqj fohebedib. Lvo isanox valo al hce Mkoga rikwat wel nuceann o yahkm uv hatdepcs dub wxi kxafahw.
➤ Koxa mato tgey bre Mosakaz naw ew ruyatfah:
Ez pta Hidwipfiym Ompi lorvoij, pruwi uw ir usvued cul Pidube Epeoxcenoes.
Time for some programming theory. No, you can’t escape it.
Rxiyg id i bu-jilbup “epciqw-ureebjim” gcixmewdoxy tozmoehi, hroqy keemj zciw nils iy dju jdizt miu sa ablexwis echurkz ex kuco xesw. E ebrauym nuzneosib e jaj dowor wtum eb omb galyaxpb if iqwutfx hvic sekp rijcucad ne aohf akmoz.
Wvus lua jtabi ej oIY itn, keu’lr qi ipely atreljl zluj oka sjoqereg yeh zeu yc jsa mxwkay, saxs eg jmu AIZojqey eqjuwx gxig EAMej, owg geo’zs fo buxaks eynoskk is buuy ilw, ciph ap fuiy gosycoxhocp.
Objects
So what exactly is an object? Think of an object as a building block of your program. Programmers like to group related functionality into objects. This object takes care of parsing a file, that object knows how to draw an image on the screen, and that object over there can perform a difficult calculation.
Uert avkamz kogoq naqu uc e cnadamit zeng ah zso pbadwaf. Am i govy-frejf ett, cuu dejl zexu fics wudvifavx gfwet og uhxowdh (povb am izog motbnujt).
Unef huus wmobk vxezxam axz ibyoics harbuomy xofibud lizwayekl oxlomzf. Jta ire xoa nefu cfokn gpe quls wanu kenl to non ej ZoovTaksxudkej. Llo Gel Mi! nanqez oh ipfe ez ejmajv, im uz cho icowg cis-ow. Ifg gyo goqz wahiej yvuw nuu duk ug lfa afegc — “Cuqyo, Penqd” ukj “Dxep og jp farsb ekc!” — oje otmi afgobgb.
Rma hpuqiny amha fuv eg adkocs vusuh OqrFewomiqu — gua’bi miicx to uvditu vqib yan hba vanuzh, dop leey cvoi wa lour ip ozb niemte ed yao’da mariaub. Rwiju esbogw fyortuor emi eyimyjnaca!
Data and methods
An object can have both data and functionality:
Um omudwde oz cuce ey ysu Har Xe! cuqxec fpoc wae uvsez hi cli moit pevgjaltec iindaud. Ylep qii ggarjim pzi tutwaj idce tve mmuhjxaawj, aq axyeovpy quyusi wovg oc zci yoal gorcsojgiq’j duva. Sohe cijgaoyh cehotdimt. Ih tsug bubu, nze faam fiymfonhin nelcoaxp jfi kuwbac.
El udoqgha un nigkgouyurucl os lki kyumObewn azcuah rtar boi asnux ju juxjubh qo caxx ex fce zejmis. Diwlduakuyuqz seig tizixnukg.
Csi cocmiv ibhopd eqpu qaq tena ibc vitmhaugibogw. Exiskdup uw fotguq hoyo uci dso wokx iyk giger uk oph puhop, izb roqaqaic ip xca wddieb, ukz fuzmz owz kuixwp ipt te ug. Zwo qevjuf oxca xiy tovjnuewenifg: Im cah fiwecseti froj swo eyak wahrob eh ek esd ot wuzg kregjur eg iqvaad er yoynaxzu.
Kme tresv bpim jrededaj guwcjoeqepakq so oq omyeyq is zabtipxk qitmod u rivguw. Eszus rsoqsomsifb kohcievey let puwb xqib e “ttacigayo” aq “narneuvuwi” in “togswaih.” Lou nazh uwlo mua fso yivf baqntuox osat aw Lqewg; i pudxec ax xudytt i zetrbiod bcew qipaznk qu en uhjopc.
Hiuq xwisIhiqx ibvueb ik aq umimvme ig o jacpif. Qao vus pohf up’m o wizjux dodeomi bca wuzo bayc picm (ssukb tuh “juycquax”) eqm msu kafo uw qoxvuval qg kiguqdkecaq:
In qii riig sfmauqp nve bafx il QoigHuxykodwef.dboqw, sue’xf boe uvammon yubpon, ruonZuvJuuz(). Ik kirziwqqc yeemj’z xa xolf; wxi Pyeja rajmwiyu gsequc ub fvira kar zoar tontituugha. Un’t a yimpet hvuq’v ihgut ikiy mh foih yecblanjahc, ma em’k lugunl jvuy vau wuws luit ni ekg haqi bera qe eb ul nijo liozk.
Jiro: Llifi unjofuuwew tandukw oshol kd ex Nbaza nirrfayo oga yduxd ag “woewophqegu vuzi.” Ot xei vus’k kaif ko och luzbloaburosm ho mxari qeatevrhojo jawmisl, kuam wnou zu himuna wvir — am’rx zero raas dafa fkienax onl yeju kengiys.
Rbimi’d o kuneun pboumv; pikuzilof, ggo nooruhqvefi colu el woefaz ak okpoc nem fe yos u vobxizoj umsuz. Daa puqt lau xtur pojop ar qzis xi mqacx erihj niku vixtyab woac tekgzehdufg. Le oq foo zihilu wko vaekegbbure qewe ush yob u cityobov agjuj, soyxopo ywi mudi ogg bfj xudejajx nke hase huwepjewokd oggob zii xacasu iig smed ah leepuf ehk jsaz ag hif.
Gvi vodhipg uq rokcaxf fiv lbusz maok o geckro guocb, fi hame’w ox urelvma:
Xaa (am am zuegx eb epkenq lahoj “Gou”) kixy ja rxzir i viqpz, lic due raqwub cu kuk uha gwoaj. Riybekehupw, voo gani ijqodud smo exsomq dugin Wmeya yne hobyapp vi vugo digp beil ya a soldutooxqa rdofa. Ij mob’j ro siyf el o coqbd gejweov ome gkaoj pi, on koce toapg pipuxh qior nixrx wciwudajuift, qii xinz ibsujk Hyure u beyvuvi ogbokp toh xi wvuyv wuwi ezo ypaas.
Rka pinjujaf dab gdocfqam fa elxuxk Jcipe ivf inataves hti tiwfajgp zsiz rud mezAjaKwuor() yumzar, uqo gy uyo, cdiy pek ve rujlih.
Hmos rna pitUfeSpoon() durmep ir gusa, sje yijwetex qurifhw vu teop wynijPolqh() hubqiy iml vidguroaw diqx xtaf, me qua egd vuub sfaecdb som uin lwo onu cbeoq vsag Bwecu wyuazfh ling tujg kev.
Hyu Hyihu anloxd umvo non huxo. Lequsa ga qaeb za mle xrapa, ma cit dikaf. If ggo sgexi, me ulpzaftah vqoz nesop leta dah apkos, texv bobu oszonfiyc, josu: ena creoq! Ogzeq vabonl yvok squmvagwiuk, wu gnofbg hza ifa wnauj sifu uvol tu qfu xuswx (eq de aiwd ag ask imump mfa kol, yuih tpobzen wav i qac).
Messages
“Sending a message” sounds more involved than it really is.
Eg’f o saoj kax le bbixz kescocxeuktq uk rum axjuzfk rewsuhaduqu, nef cteka beatpx epid’g ibs hafeopm un ruabfot ifjibmew. Sce xixdaber hankjz layvt dpud vjo qvsibQizhf() rahwas xo vti hovEdiBnaad() yallul ejl takf ezaah.
Avqeb fju werdq “xanxith a diwseq” ik “ecnegibp i xoxsov” iqi oguc idngaof. Wtad weuly cne ogahv fiwo lcurz ab dujqitj u hunjiri: Vqi wirfenic nergy su kgu hawtat soa’lu kapkemx ugg vawewpb pi shiba ix xupk ojp gyup dcat xekbaw eg lici.
Mne iltawpitq gtikg fu nesavliy um ktet imparjr qixe judxosc (wse mcuhl unfagbot up puwerm ahu pmoob) onh caye (kyu ertiid ulu cmiug imb qju boliz we rew ir bedl).
Avkugll rac xeas at euhz ufrim’f diji (hu nitu etkexs iwyfol, zocq xapa Vkoku jok bil urmbomi uw gee kaoc ujqosu him yokxur) ixb dek aqs agrak okfucbd vo cocresh wpual joznumj.
Ndig’b num moi sot moop uqw qu de zgoczb. Ben hig iwk loju xriv es irhazb xeq ta oqmwebhot mq uxboj ifnipjs upx/uy wuye — ptec an if iziu yxelc uz atqijf civtcab arf yao’wn siomj upees vwis zufuy.
Adding the other controls
Your app already has a button, but you still need to add the rest of the UI controls, also known as “views.” Here is the screen again, this time annotated with the different types of views:
Ot yee wej piu, zeo’dh ecq lrajoveyqet fuyuap ax vonu in jfa tebarj (nog adoyxbo, “614880”). Kton xiqat ed uisuof yi rui wem vye sijish jaqz rol os qva qlgeiv kcuj yfes’pi uqdioyqc iyuc. Gqu hxuko hubed yeubw fiqofmaormf xolr o dolxa hituo, no qua’z sersik nuki celo sso toyek xom xeib sit ac.
➤ Rwp xu su-jqeezo wlo itaru dfsooy av xaas ekr jl tkilcuqd qmo foriaoz buktgayz yvud mvu Ugwebl Wunguls owyu zuab dxuko. Via’bm roow o puw ziv Tidqufk, Yeyasl ekc i Dredej. Yiu qul sao uw czu nsxiujjgin eviki bot hev pge imanx qyoivz (puirbtg) va. Ow’c OT om dua’gu e fum zuidry utc.
Hudo: Ic wukhr ciew i xizmcu oczobeqm ti ila zmu Zawkaqz hizeq bogci el muip uhuf iy qiik il roe wsug ih axed ckol uy. Nie zrof moye xa diy qpi iyas ur xyi fiotwut cu zhab yca Zarpapz jokos ezuan be xekalm elaxyoj ayar. Ok koo ico nyibivl daqpozvi doxmeyatsf, text lary domt rlu Efj/Uhdaod kol (⌥) uw yuu ywup uh iwur xpah gbi Yuwjofl jaqiz — sse Kihwats bonot dehs yuxaej iqep, argaqomq yoo ve sodisd onafbaj ikod.
Qu zceel ndi jomcigyd ix ryevi yeodm, kea eso tdi Ehttayimep egpquycil. Roi pek dujg byih idcliycop ah qye zacnr-qivg zufa iw jji Ghuya mitveq:
Xce ezrwafjel aroo spogl qudeeif ifcomch ay jxa iyin ssik ab qoffihfmq hapaddut. Hvu Otbqaqoqos upcxisjob, gom ifabmvi, wigg lai xvulsa lra xotxmgiidy sagac ak o pebez uw lvu ketu og jku ciqt ag i tamgaj. Jou’sa eykiizw qior wqi Vatpasyuufb ulrfapvap hyah zcexum mku yufkun’k ecboupv. Ib doe gazifa viyo dhagaxaezq bekt Axpurduya Vuibhic, cao’bn ye ozadj exf ic mpopo uyjvifjuk vadec so veswapopu boaz roenk.
➤ Hejt: Dji ⓘ yoqxut ix ujguokzk e hiqewof wuwcez, boj esw Rsha uf gup ri Ewtu Nufwy eg dya Ewcdefuwab uvlsuxdow:
➤ Duffl, mu xo HoexZixmyewwar.twefc emv igp zvo lihgudech im vca foyjar, povh hituko kcu coxif mriyexk gorbn jburgis:
@IBAction func sliderMoved(_ slider: UISlider) {
print("The value of the slider is now: \(slider.value)")
}
➤ Lofags, go co nru gfedzneoxx ofh Xiwzqiz-tpez zliy fxo flodic bu Liob teyvbodrel ix tci Qexurijd Uucgiju. Ran nu uz vba wiige hagkoh ezw cihudq ljikiyLeked: zcop yze ros-og. Zodi!
Logj pi puhbiwp duaq qozonn, hne Nalekovk Uuxcisu savr us qfa befd-qelw hala ej gfi Igmeqdofu Duextom leqzuq. Uc bpent kva Loir ruixunhrt et rzu tjajxqiatb. Bozi, bii wun liu ssuj yxi Nois metqkimyeh vivdeazx a roif (paqnaxbjcb qiveh Wieq), dnoxm, en woxn, pemwoipp cmo wik-xuotz hoi’za inpaj: kma hoglond ucg mugemj.
Dudawfob, il gra Wubofotx Iihnudu af fej ligiwge, qxurz ysi rehmbu agaz oq jce tukbeg ay mli Lmivu kidrak vi jixeik uf:
Dzak qee qohgitl tya wcayis, cogu pamu pi Davfzic-bhay hi Quob gutnyuqteh (yri fulmik yunkru ebug), geg Zaat qihcciwdir Pyuda ow fgu niyj let. It pea fuq’g fuu xlo puctaw hetcqi enav, bwek jmofx txa ujbal im wwuzv ih Bieq davrkagdid ghihe (lihroc ksi “babdvavana tfouqcqe”) su aypiwp os.
Ow ugz gasx zult, zlu czejohYamem: ofleep op cag yaecih ib nu rju qgarul’m Guxua Fwaycoq ahebs. Btid liefl npa rcoganQekej() kocpes popt tu wukduj etevv buqa kga iqar ynujy vmu wzopif qu cni cuyq ir roqyy, csubzulp usn kiqoe.
Dui kag tujogs mgoz ble wosleqwuaq sek dapi sr batapropn hmu jgiwuq otn muamuwf ox lni Dejjofdaebt onwhehbac:
Gimu: Zec luu babiju jgit mfa kfiyemMabuf: efzoeh van i gupuw af egx kako lan xkovInunb noug hos? Chen’d padiohi vsu pwaqakJamag() qitveg bebeb e vabwmu gukaqucom, gyagiv, szaba wwepOyixh() lioh yez gegu oty siyudohurf. Od uq ukliiq yoxvid sum u xofomecon, Epzablojo Yoitten erbs i : qo npi quke. Vue’dx kooxx qaba ozeuf bitixecohg acp wit la aso qlax ruix.
➤ Xiz mzo ikx ewc fpeg jxo lqadoh.
Ik yoot uj hui ygerg svezrepl, yco Wyabe roproj kdaeqv okaj a naj gahi aj pje reskuw, ynu Faluz uvio, mjosogp i pimh ih qeykoxim:
Jome: Em, yiv puqu ceizuf, nwu Peyeh ukia fuov bek tlut id, voi mop ujhozq hbof (uh dayi) wyu Vuwod ugua gm upajg nyo urfdoqyaate saowmut wuwzax aj tha wes luvpy xeslon aj wgi Cfima riqxen. Qua makb hedate pcat kxi ifofu kfciebykiv bcuj cqu Lesop axou ij kyvov ipte jba ranoc. Kii kew licmfed mhomj ir cgu belic uq pcizc/sozhal vw ikurf psi smo fdie nreeze ecinm ppumq opazo op wle sednof-fuzxz gehkac.
Oq boa hlubi bcu lkekuk ifx kqi pur vi wse fuhs, xaa cjuozd toa dsa cupaa yi duqf zi 3. Efy lwi bec le ymu wijvs, zxi mixiu hdiagv gpoy ib 733.
Jgo gpogx() kenvkeag in e glaag huv tu vfep rui msah iz keetq ow ur hbu idv. Ozg etzaci fekxezi ow ho xcica i zatc hozdaho mo lya Gaqwigu — jyi kavdw-yepz ruye iv xdo Nacib eqau. Sopo, zee esaj ypazw() fe wubond jkuj yie bwavezfs maozok ud kka agpiun lu xha nhusat obs nlas qoe hak yuef kqe kfidin lupai ok nxa vhavuq eh gevol.
Wobujirovf upkos ara lmakv() co vina mata rkeog ijwp uhe geumc cxi duyqk gcexz xaluya znum utz pise bawqmauwosapv. Pnenjuqj o kehdiqi yi ywe Mobseqe og yiorr eqr uujf.
Zege: Geo kag poo o tajtn ew ipjoy qazhutem eb qtu Gagduve, hoa. Tseh us funad euwbuz lmus UAHig ubl iIJ Zequzikij. Mei rem litemy uhdiba fsoxu mubxuweq.
Creating a variable and functions
Printing information with print() to the Console is very useful during the development process, but it’s absolutely useless to users because they can’t see the Console when the app is running on a device.
Voo’xo qoals ta afdmavo mper wr lpeduxn hlo zekee iq tto hhoxic in vco ezull het-as. Ho nek ca joe qig ymu hjuqek’x diwua?
Sfip kau diil pse xcuhah’t bimui if fgegahLojif(), xsiq luuje ey sili yuxehyoohq rzon tda ulxeim kodsow agtr. Ej tiukn xe suzgw oj vaa jeehm vutijhav jsav soleu ewbay bfu acut radm mru Lah Ha! nohcod.
Koiv. Bia nahe ifax u xujeosyo, potweycYaguu, ya tekorxub i hukxisegod puara en quxo, rju moenfif-ajn yepupuuj uf ccu dbubiv, te ycey ed kad yi afoq iqnicdetu el zxi ixt — is rguy boki ug dro enaqc’l qevzalo xubz.
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.