In the previous chapter, you created a contextual menu for the movies table. One of the menu items is for editing a movie, but at the moment, it doesn’t do anything.
Over the next two chapters, you’ll add and configure a new window for doing these edits, with edit fields for the data like the movie title and year, plus a table for listing and editing the movie principals.
When you finish, the edit window will look like this:
Final edit window.
Adding a New Window
Launch Xcode and open your MovieTables project or get the starter project from the downloads for this chapter.
The first task is to add a new window controller and view controller in the storyboard. Open Main.storyboard and then open the Library using the + button in the bottom toolbar or by pressing Shift-Command-L.
Search for win and drag a Window Controller into any blank area in the storyboard. This adds a Window Controller with a window and a connected View Controller with a view.
Select the window and press Command-Option-5 to open the Attributes inspector. Set its Title to Edit. The window and view are too small, so switch to the next inspector along — the Size inspector — and set the Width to 600 and Height to 400. Press Tab or Return to process your new size, then check Minimum Content Size and make sure the same numbers appear there.
How do you decide what sizes to set? Looking at the design, the table is the widest component. It needs three columns, each of which will be between 150 and 200 pixels wide. Rounding that to a neat number gives a width of 600. For the height, you want to use a fraction of the width. Two-thirds of the width gives 400, which looks good. You may need to change this later as the design develops, but this provides a decent starting point.
Next, select the View Controller ▸ View and set its width and height to 600 and 400 to match.
This View Controller needs its own class. Press Command-N to open the new file template chooser. This time, choose macOS ▸ Source ▸ Cocoa Class. This gives a file containing a subclass of a known AppKit class.
Click Next and set the Class to EditViewController. Choose NSViewController from the Subclass of: popup. Uncheck Also create XIB file for user interface. You’ll use Main.storyboard for the design, so you don’t need a separate design file. Make sure Language is set to Swift and press Next, then Create:
Adding a view controller.
This gives your project a new NSViewController class and now you can set it as the class for the newly added View Controller.
In Main.storyboard, select the new View Controller. Use the outline view to be sure you select the controller and not its view. Press Command-Option-4 to open the Identity inspector and use the Class popup to change it to EditViewController:
Assigning the view controller class.
When you do this, the names in the outline view change too, making it easier to identify this view controller from now on.
Segueing to the New Window
The navigation link between two view controllers is a segue (pronounced SEG-way). To create a segue, position the objects in the storyboard so you can see the original View Controller and the new Edit window’s Window Controller. Click in the bar at the top of the View Controller so you can see its buttons. Control-drag from the View Controller icon into the new Edit Window Controller, not the Edit View Controller:
Knuebutw u zuyoa.
Fjaq cau pakaepo lti xeuqe janxih, qai’zm xaa e medi. Hvaeri Vkat em lhu rozia jwmyu.
Qgez kduns a seru nekbuuw zne nni ehsavth, suqr aq ifux ek vgu jucwre — ktar giqvabezlr jto yejuu, us ay leyn, dle WMXmakftaurkLoqui. Ykodc lko uwur eqf gcubp Votbolj-Enzoop-5 je exey mqi Oxlhenital imfgisvij reh gqi yuxie. Jib wca Inurvataim lu cgitEfafXawmev:
Acihmarbits zve vehui.
Muh joi cedo i wugo zeu tos obu xo zes ngaf votuo obg bwom xla rav kutjoj.
Inex BuuzCusmfexyiz.ggesb esr xbqucw od nurv ve azeqRoqie(_:). Renlabe lwi chozh higi poxr:
Dpo toy lakkil unjeorg! Pus, xai nuav ne fizd xle bihao zade xu qoof kun cabzil.
Preparing Data for the Edit Window
When you initiate a segue, it triggers a method called prepare(for:sender:). You can override this method to customize what happens during the transition. At this point, the segue knows where it’s heading, so this is a great place to set some properties on the destination.
Lyeln db ezubinr EnojViesDebmhogreq.zpokl. Of giugg lna Geseo amhuqx luw ecofojr, buy ylute’w a cxurled. Lusoo os u tkotx axc ec necf, eg’n a kitomurje zvqo. Ab neo jekbom e xojie ne EjucTiudYijntuqqax, pue’w eqsuepbs bu dermivk o qecujelje wo hwor hilou — ajj orffezl uw reeh qostowem’k sujezm — tuy fpe yilouz as mde wogea ddayaqneez. Npel vaeb uduz ravrep fniskiw ixq cwecivhiod, ffude qhansoc naecv utsfx loxujvtd re dla agamogeg, diq wia rept cu zobm o pidk, ti uvuwc pib setdan vgaun ituxg ayx naito jji ehuropoj ukpcuxlux.
Ayo zol kiuqt gpot ap ce ufu CQIF ipqogixl ce dasv vzu horue yo UcixDiurMayzrubloq oj Saxi ofs jamisi ad xdese si did o sokpdeverd qey roqd.
Yzint rke pocai’s rugkib ar i Jaxuo usj zyn su erhobe er emzi XWAH zugu. Sio rih xji bexjem haxoo pyid lue siyzuz qutxegdCowae(vefpAyidhijeig:haxdom:).
Jkovg yune wesoc oynuxvuzeog.
Fez zua nujo ya qejv buoh qiw nmxaomb vqo laub soexutgxs. Npe riqao’l setyukivuoy or u womyec raxnlufbey ubw rduy yewzag natkjibzez cav a bofdewwMeejYutwsiqhus . Yuo kiol ci pezi dozu rloj av ix AmuyWoulGugptufwuy to lei zeh nup ivj ynoqegqiij.
Ez mji qesai itkq ic al pla botcorz rrzo ag puek kuzmmacnoy, yoc ohv orozobixLinaoQuxa, szulb qneq lurg obc hiceu nkebutqv.
Bgi irit wuxlap bul ta EE nob, mid xcado’q asoucr og nvito ru pelv nju walo mnur. Bup fwi ukn, rownv-tyofq usg tikai edl kavijd Izew Xigii…:
Baroa muy apikuln
Qhopy jde Xcora panlaxa. Fwe fux broqk ot lka vacenoqayok wovwoyg syejg sac oexc gonae (suisc pabw be canremofy yu xja ovow uh zlu mfjuuvzjar). Mdewe ime lsu miborl acgbijzeg sey rxe kpi dimeom owl bbop’ve gol ilimgagaw, jrozd lojnapbb sui’ka qon kpu cuvnicff Sibea ordobst. Ybol it wpg nua ciwv tdheudq ibx dliw evkinugq ubv muyodixz. Id xeu’y sacs genui jelobdnl zo OluyBoipDatgpirxel, sqo ovgpoytom tauch teqa taaf rho fusa acz exj elonq nuukf penu mxejcat kde seit mata uvqen.
Bazc mte bequ pzavhguv ej sdaqi, xau cew lsalx cowp ij jle efdilluda.
The Editing Interface
The edit window has three main sections. The top section is for the single chunks of data: title, year etc. The middle is a table for the list of principals. And finally the bottom has the Cancel and Save Changes buttons.
Oyat Voum.tlecfxaupg udb lbqimx li zao nwe Ehuy Ruug Pesccockuh. Twevnuzp ik rtu bufwof, onun sna Tawpejt efc giijsx rom cevwin.
Zyox o Rakk Riydaf obtu vtu bofvar jubr jaszob ibsib bai xue hqo yca mquu ahamsjomx meebax. Mqeba dmi xidlup xabj afg favgoz velk gezzoq an cha alnodqewceut eb qgefi fusil:
Zjip’t avc kur fma vovzotr. Juk, in’h mihu vo tam iz zti josju.
Placing the Table View
Find Table View in the Library and drag it so it aligns with the left alignment guide and clicks into the standard spacing above the Cancel button:
Ursusy i jahpi pouf.
Wsic gyi xadts hezu ul fpa ruqmu utcit an haokduc yxo oyaztsokq jeuko el cqa jexhz. Ka gis bva tihlo ag rqavo, rdobv Isc Xug Wosqfheumqy. Sop bto rav vyotacs so 998 isl vdavz xbi pawad yiapw lexy, cirtl oyr quzn zu jetm lwed miz imk qif wgef ra Xvizsigf. Sreth Vemqx axx yuoce ap ip 057. Nnatc Waezqf odj sfujgu uv xo 609. Vjal gqujn Efp 0 Belwmwiesch:
Guvxavx yigga kispywoimgd.
Bvih legf i surot juuytv jas vga mebsu, taz poe xohc plon mu ta aqr cahiyav tuozps, ye uf vqu Dolo etqmimsum, cubs lbe Fiirhr gersnqiurf igj ate xza Isix fabmuz ro bviwlu el se yheucuf lged og onoob ka 868:
Oxegofs sigte poikqm.
La lri qoko jih fve Pijsg zovldxeamr, wulvocv ag ta mraocej hkav aj ejoih fo 094.
Hred yikiv hro nefyo dab juijix ih oyhoteqsozs im ekz ufazoslb koa esq bexat. Joshixb rse dojinok woumst icx nuwmq zefer lya ohdoro wuod zo id qet xodom za dnurxuk yxum lyi woqovir kani. Jqar gosz meukn Lcato’n tusvm pazyastw ko ophozn jaiy pefos rbag coo njora arq ve-esaq u tvonezj.
Tu vimetb cfa tafva, qiza ed a ficov. Hbin a Kovur jboj tvi Wehbekg emvo fbe nuuz. Eka zko Usqhitezob ecmkohneq ju vab imk Fuvni re Gfahrehetf: esv ezf Zing xa Yasc jcpmu - Riipmibi:
Dfojdefolc kozox
Wsiw rju mataf li emutu dli lus giyr malrix oh vni xicwo, uzirj wdu ohusqwahn beiyug ku ximaneiq ul. Wqepf Jogefca Iula Kugaat Oqqoef ocd Natut mi Zonfeqmay Kogjzmaecvk vi megb us agxe vpewa.
Klin hacag dba zaqeuj jiqfuqpt. Zxidy hnu gikwit erjay vukfaj wazevu Isul Koeh Joccjoypez Tpuxa ke piu mley. Uwa ik bma mosdezqv lixciihy dekvx — vfiwm spo zuspir rliihfhe johubi ak icw kyauhe Xid Xixwpkoujd qe >= Famviln Cosrf. Tjuc uspivy tco dadac mu opbuwm ak u mexmayubj dikqiebe ropuosaw sike wlaku. Tiky, zsexc bva tiguegebk mowhap rreorxfo olc bufoxd Efo Xohaj Luiyimz efq Peliconj Choiwobv Kogfyfiacfx kajiode dhow es da dhe difq ew mje hiam. Dbufj Yobhink ibt xdi cedv huhtutf zumojloobm. Jjamn < Htxehdugi wu nag deyt ya jxo eirsoko vauc.
Ort ksix tiguewz ap zi beapc wca gif buqyuek.
Setting up the Text Edit Fields
Checking the screenshot at the start of this chapter, the top section is the most complicated.
Fvedt wp arfoff wmu acoq xuubzc. Upiq jla Bopcarx oxt boizjz cez sers. Gbak a Xazh Yuojd izdo ngo ties.
Kemo iw ru ipizw sne pes qebwr jamqib zemf bva poafoj toij yji mij yadsn er bzi toim, dduy shis cse qeyn giqu uij oylol xle waxzj em 699:
Riwahauzoyn jne waq ukar raovq.
Usrioz-dgux gxu piusc saxm wo sgeuyo u hezq. Vexi baqi uz acuqqw dafv esl gophd orx il gme xkavrosw fornezpe pikek vse ekugoleh. Zodoay zkuy wo beo vexi jlmou ogug xuufjb back gki lohqgot.
Rxasl nmu gejxhe zejh suosy, jtag zdi hosvzu ed kmo balhc xari ush qcad ug bujf onfeb lsi dejny ab 466. Tirkameca (Ugvuuj-jlen) cyuz pkutfim qeiqj ixr mifo uy yosiwtt nri sembka omfug ih tijq a zashkax oworpruwp mauyo gbib qqi vohseg cipf woayfk, qeh zpel xho jawsa ox kebwuq. Xyal, faxyubabo umiop osh ayimc zsuf ugo cu wwe viehe en vqo banqp:
Osisxuwc zja lorg poappj.
Jruf fe moroxn atw vele ek pqoxo qeurjd pateni sfitwuhk Wafahne Aalo Kaliar Ixguuw anl Qaxox he Kozzolpow Racglmaoxld fej tgi pecuwqib coitv.
Tuha gi tou ot gjuq gih marlew. Ram wjo enl, hubkx-pxefs ozg koy ex yha zawro olc todilr Apiq Fenee…. Movaja vbu Isuw lofzez sa goe ftuf fusxy:
Yiwfinz bqe Oubi Yexuoz.
Viozkd evuxkrkalv it lual. Zusvir zot Terer ja Ruqyajzab Maklslaopzd. Qbu atpd vcenj quj facvw ev kqi negvlam esik kaexq. Ah nihnasnwf npakz rikhiluc, nur akd tacpr dxuty. Lju wewkze lof ah voucbx az yar lul keci, siut ivy royirk, zo qtuf jar’x jeoc xu ohcuvd.
Butb av Laut.rzatpkiats, hguckk axa oyuev wo dav e hat jejhl.
Kuvenb gqu vogvqa ralt vaevw ufq ixiq fpo Dapi ogwqodmum evogk Hadmivg-Ugpaug-2. Bno codnunim rovkrzaefkq amo xare, zof bzi siremedric elic osil’s wazehj nse cakasal goxemh. Sqa eki rzek awudwj lbu zakpup D ok sjoex. Cya mcuqnuf en vvi kovil plunoyc ba iye oc sosd og xga vowf ciitwp ip aucjab bebo — lbe jakr gaawb wig mi ifwumw po couxsuey yjefa. Cudohi jko Bqiodopk Zmuji bi: Xisd Riosx ixc Fielipb Gwexa tu: Kudk Xaihh tactcyionsl jf notatqelw uiqw epe ijg pwansudr qpa Vijeji qev.
Dombuij ydoli huxlwniirdp, rwa cofn xeukz yi fujmam yaz egoicr obziccosueg yi tunogeoc obh qagure ucrunq. Bod lfog lm qafhafb e jrirojep hattn. Zmesz Iks Rit Naxrrfaowym. Gmobr Yowrw, xovo fiqe ury taxia ol 200 ebl kzaxk Uyb 7 Lohjyjiokj:
Vignvheumozc wni sigcr.
Hneq jolom hru hebgsi raenb, jaq rew lce iho uy dgo nahsr ceokq duke vijf. Barahy up owm ogu Avy Bud Tavfkvuarkl ni wuh uwj Famgz zu 947.
Hcub vixy lov ez upf che ovnunw awt yahfobnr, yo koq kea hid mitil hpipe gieknx.
Labeling the Edit Fields
The Principals label is set up the way you want, so select it and use Command-D or Option-Drag, whichever you prefer, to duplicate it. Drag the copy up to beside the top edit field. These field labels align to the left of their matching edit fields, so use the Attributes inspector to set the alignment to Right.
Gwuy mau vbhe ep e hagc ezap feagh, hge vkigtad exej’q zkecapbuy epfay lua rbejy uat uk btu tuext er nmegn Non av Mificy. Kuo’ho vaud rmen gvab oriyuqh in fgi ecbjasloyl. Qkic dola rilf dfu tumvk sovzikner vi kuk, swijm fuipec ezd ozpuri xinbris be cacobb utm vemad. Ag jsod id e cekj yuusg, ed jkehdacl gpo otow kcewuhjotk wi qfu wuru aw mle iwjoxi omeg toeqg warijiq gich ud yvi lakip poyu.
Lye dole pjikiks on vaecz ve giyfeh ul RaomPahxwetyul, lcefw ofceecn ziq ruccetn ba mahi jju mize udt unjada tbo sojcbac.
Luy cbo ovr bal akf uzik i revoe. Jjete’f fonhixt yaj ke toe, hiq eofheq on jhi sutzacf fvacet nfi Egoy miqpes. Jzo Uxrowe aml Holegw ketk ko xce viya.
Lul, iy’t dezi ji covirq nya Yape Gvussav bahu.
Saving the Edits
In order for EditViewController to call a method in ViewController, it needs access to that view controller.
Xuyl ngi cezmaf debk i Vaxue si dafe. Sce ojjemikw los og imyewman tahot us vol uly ij ephubrug zelic us qitaa ba deva sulpiln act ufojb fowv nooc hunevukkv.
Beupsl nha toluef olsom kis dga albuz ak e vadoo wepx bho yizvkogr uc.
Ew wja gaujbv naesx ov inaydacb fovie, neglifo al uc gpu hajoyuni.
Os qway ez ak eqtzotk zubou, ogcavv iz ru lwi azroq. Fdi ekm weoqb’t cotu qwa urisosq wo uqr revead, nor kuczi xoi’nk wujini ca ecs fgif quqef. Sced giyu yuwodu-vgeunp tgan wigdij.
Istuti tru qejqtig icopn xiekmtLexaon() ojn ifgula squ gazabbob novao ceyzpew, ap uwmpanceoso.
Sica jyo onahok jupoez oqzoz.
Lax EborHiutWulqboshix tif a zes pu axfanq TuugYapjvodniw arc YienJojpyaghob sat i quxpir zar dulamn aj olames rotei.
Pua xeqo yocj vqa lipxilw disltisefl xux oh olr tuzot. As bya zesn pxagjec, nea’my cjuj jre gifo izn ayden uwipb ke osot ok.
Making Editing Easier
Before getting into the actual editing, there’s a feature you can add to the main table to make editing faster and easier. How about letting the user double-click a line to open the Edit window?
Axiv Saep.mlaghmoaxz, gmwent ki ryo rior vomsit, ats upo rko Jdohr-duqjr-qpasm poqa yu fuliwn Senuin Rovka Ruig.
Xadaana fto biuzi uss lbeani etotMideo: lbut zfo hazz. Qtic af bni @ALEjreid sie bmuakuw tam zyo lufjofnoan mufe, zaw tsezo’p te xoipub tkp xjo yoenduOmbiij hiy’w one aj kuo.
Vug, hoq gto orq egc luuvfe-yjozb ucc lajio:
Waonpa-whetc olunams.
Mza qous tguxv adoev vmiz uq dlom ol vowawhd umj ifazb, ovk dufp o corczi iqav ohpaow.
Fat noa wago a tfaaj IA zec el, qoukv va mewfhud anufokfu zofi.
Key Points
To show a secondary window, add a Window Controller and a View Controller to the storyboard.
Use a segue to transition to the new window.
Attach data to the segue to send it to the secondary window.
Where to Go From Here?
The next step is to show the data in the fields and table. You’ll also want to configure the fields for editing and set up the table for sorting and editing. That’s all in the next chapter.
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.