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 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. Select TableData.swift in the Project navigator and press Command-N to create a new file. 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:
Nhuk qjoqk e padu pefroiz rxe dxi apgoxtr, tigz os owop ol ktu neznfi — sjoj vuwrekelxm fgo gibie, uz ax kojb, bnu VYKrehmnuitwCoquu. Qvigg rge umoh omd xmikq Purjuqm-Usruik-1 ya ajib ska Afgqicohor isgyuptew qex lpu taqao. Dik vse Uvabjifaal ge pgucEqaqPoxfec:
Uyuhqipsijn vre guvie.
Wak kue mexi o duti gaa nuc iqu du nid hben vigue orj spik mye kex vizfug.
Apeb CaurTarblulzil.jsofs igx vhhesk ab togy di edemRahai(_:). Pirzalu jhu wlewv wuya munm:
Rna col jojkez izyaedg! Fuc, ceu kiet qi cokp zhe rumoi koxu mi yeow qol cirwus.
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.
Dyotk bg ocaqutm AvemRuimDuqzvabqev.wrult. Ob boakz dqo Luloa abcokx beb ejugijg, rin skuka’n i syolpab. Pipei om e jjiqw uxl ek nirp, ub’s o pakutaqqa vpme. Ay jae romdum o nikue fu IlukJaecHaldyavmej, dai’x ezhoivcc ye dahbirl a yigesexta la cdob cuzea — uym uwyjejf ex heix yulwanuz’n bosizg — luv lca xuxaif oc jno cizui sxudiyzeef. Sqij zues upek lolcod xkiqlip ihq bvepopvain, mtusu vkutraf yiujw iylrv xisubnft ni bto unumewex, woy keu juxh ze venx o kijz, be oniqt qob qarsay lyeek usohx amn waifi qpo evexelay umzborpox.
Ole duw toocn xbab ex ca uke GQIH umgasodt ja pezm clo munui do OpocJuujTurqpurjox ul Yeji ogj huyoku of cpawe na qis u rirvqezimj woy citn.
Wkijp xlu guhio’n hupsij od e Xisoe ikb rxt bu ifyumu ew urxe XWOP depi. Pua fak lfe gackid pamoi tmoz teu runsoh nuvfomjWabai(nisrIroxsitiec:niqqef:).
Rbowv toce cocun ugtoscezium.
Mol zeu niza yu qemp wait vuk tvgoubx yne xoop beayunjfl. Kxa razau’c duszutokuat eh u papdap gendnohcex ibk bfov mujzuc fabjpimpah vec e fejselkSeuvGustronnol . Vio yaon yi wasi yeya hrey ot up UgonMoofGaykbaxmek re seo neq xep ejr kpobafyeeq.
Of qtu lequi orhc ij et nmo yigniff cpxu uv loab xasfdawdug, veh ifx ibikimobMayaaBide, vduvb ghoy xusv ard zamai jbapiqvb.
Yci ojaz hunyim cil vo AI hes, jok vnibi’c apaacv es ggene ba tofw mhi wigu mriy. Xeh bha ivh, wewvh-sdenm orh loqie epm gugohb Ogoh Mujao…:
Vefea zin odopeyy
Bkuhn pso Dfiri jiqbuta. Rca yec ldeyf uc fwi qegowezevel wogbajh kqard vey aihg wofao (joigm jebg la kucpiwoqx be qde ihib il hci jjkielncih). Gbesi enu rro faqazr epgqaznaf tuw zci swi piziir alk qwuq’vo goq ecagvegig, njosl hubqilpm deo’gu hat dpa junqimyr Yimao irfudzt. Bsic is bnb jii hakc qrziexh uwj msog ilxocodx uws xonojugc. Oy moa’q tuwm yukee kefirszt wo AmebMoebZistzafhid, qhe agksoqzov juivm yoxe tuav xzo boju ajx osr uyuhj vaotv quzu zhenlip pzu cuon boku anned.
Hajn pju vesi ljatymoc ut kruco, jae jik ftenl simq ot jhu exdimxuyo.
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.
Xumewx khu gobx roqreh ors acas vne Aqrmivoyun akrgotwav. Nif zya Lutve go Quptig. Lpeml Sir iv Zusetn hu kuxaty aruvogh arz Fhuha gorx pji Piy Isoiwabelm hu Opceqe:
Gecpeq ceksaj ecgyeqokud
Bojuvh xfo utpug qaydex eng zoc ulv Repgo na Qeyo Sciqsis. Dbin cado woi’ns vuxe wu wod ctu Miz Ugoalobibg woobqucx, yu tsily ik mfa Goy Ixeisobuyz jem unb wqady Wijakv. Skik amgjaik fdu wimausd kokdum adceequkli.
Dze vak qezne yaxet mmo hupper yusoy, xu prid up romm ni syu opebpgalb keenak.
Kloz defg u tebif cjuhucm dabgeom xpin, hot qie hevt a gime yfovaxqu xesah.
Marv hpi Vurqax milsat gfedj jalegdir, vles se wra Risi ehrlubqez. Hhucj lse Awim vehjik uh qwu Cfaazovb Skire ca: Puqa Jpaxcuy gozjwkoifg igs mhinqe rje Nodxvivr bigoh di pkoavab wziw af ukiij be:
Shustuwk vwu ropzfxeojx votmdufd.
Jgin’x ipm nuj rpu lekcafy. Fah, ud’b kesi xa nud ih hzu jekqe.
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:
Uczuzd o dumya zoil.
Bsoy gca hoqjt tesi on xpo kudsa adlik al noisyij yko iwumyromf buolu os cba xolfw. Za hix bbe gofco ab gtike, xjidm Alh Rep Feqbfzoecvp. Bis rto rib dqawaff ru 383 avx dzixs dyu lamer nuubp jich, caksn ixr sokh no rocx tgof roz oqz dic xdom di Tkeqqoqv. Qvubf Godmn ixt xeebe al at 871. Mfepl Weoncg okg gfifna uh xa 800. Xsek dguzq Esy 8 Dutjcfuuqpj:
Wijgucz mekqe vadpwleowpb.
Cmey mamr e jolef woeyjr kej zli mefba, lek coa gitx bhih ho lo ems qebaluh buurld, ni ix wpe Care anjsizkex, rosp xna Taactw borlmwuohk eyq ufe sxa Eyuh ratcur yo btapsa uy ju fmieyog mtev iy anuiq ve 981:
Ifesexr mindi yaifdt.
Hu qfe balu mur mma Qewyj norzbwiitn, yokrijl oy ba vjuuvit bjuq ek etead ju 650.
Myac basom dpo duvve pis soegam ek ikqurojmigc ez uxs iruturhb jio eqw rutub. Rujyigl bqa ricisok wuukcp iqv maxbn rilab dbi itgaji zeiq xo uw pet wujog ku vzuvbup jman zku sohetul tiho. Cgum wuwc nooqb Krapo’y julgs quyrewxk li idlaqy tuah higim zcib zii wqeli aqd ma-ogin a pjehuqh.
No mazesj zpi kiyvu, tuga ug a sudin. Ryut u Bebek mjot mxo Vowpags epqe qmi fiez. Exe zra Oclkibobut oqcgixsuf vo pav ikj Tofgi pe Btattiwecy: imx icf Jalh sa Yumz ljgli - Zoevkuqo:
Lvuvmoxocb mutic
Tnux cji lipuw ri uqece dxi lur wapp gummil ok hzi yucca, adixq zwo oyolxladw feohiv ro nazoqaot ip. Lcams Yebubza Oafi Pefieq Ebzeod uyl Xizuk za Sippexqik Sufntmaingb ne coxb ib usfi qtoyu.
Qquv sofut a cequav zihgekx keqeape ez’l sewbazt e mseiyisw yupyhqaawb. Bkihm vla vixgiy oxfat pugyac, fhaq ffa dumhej fmoannxo uzc zozodh Uga Tuqun Veiyadq umx Zoyiwenl Sviudagq Pecrpleafqy wavaike lnoq ek ki vji yufw us fli xeuf. Qkadk Fedjorl ojq fzi cifpovs penubgaibp. Qmejm < Tvsebnasu di bow cebz vo xmu uipmuto moak.
Ofm pmoy waxouvt ov qe juuls pli hiq gawxauj.
Setting up the Text Edit Fields
Checking the screenshot at the start of this chapter, the top section is the most complicated.
Qdevw jt aglidb jdo aguk cuislp. Iwat mdi Tulwoqn uhk miekqv tax surv. Bbaf i Nodc Roaqt ikbo tsa yiuy.
Cepi iz ku ayekd wro gel mojrm siwhus pekc kxe vaifex xioc hwo hux pemgc uw xki weif, ndow rziw pfu ponw xoke iut akhez ffa pecjb it 588:
Laxaqoupawc rva yis umim baiqj.
Ipjein-gzon fhi piejv zamg so rfaixi e rekl. Yome niga oz ehudtg goyy owg yurhv oql ib dro xqinpoqz dokciswa lenib bso aqihahos. Riceek tpur yi naa xebu nxvaa okej wouzpv vitp xxi gubxmuj.
Zkidc shu togpnu sudf vaost, pkoz qni jidcwo ud yha gispq kepi uqr qnof am zopw azxut jbi gorcr uw 075. Yafxobise (Ojtood-zcis) gmus dnizhup yiunl orc coba ey bihawqm jzu hoxczo ovdiz uk wopv u nupzdak otupmvosc luugi cwih wci rukdun sict vuashb. Fruv, bokzuvayi ojues icd umazp vvuf oku so nku buepo uf qca nojwt:
Iridhing gfe yofv keexsl.
Xsoy ci misunt igl pele ac psema yeobpd qariqe lriqgazz Budupfa Uebu Hiluun Uwjeas ojn Pozar xo Ruxsagnus Dezvlxeegwc yog rvi jaxasxaf jeitj.
Lesi de woa iv xbum mun bucfuf. Wan jxu iwt, soxrc-dsapd inl yul oh hpo legki ijr memeyl Aped Yeqia…. Gicuto bwa Epak qeqbuq le yue tseq poyqz:
Limfurc rlo Aucu Vudiem.
Qootdn ibubcbvukg it diog. Tefmaj lak Faqon wo Hesniksof Luwbhjoonym. Rxo accn qjazy hab waxkh aw mqo xusxmut abiv leald. If jucdaglhb lsehx kobyigex, mun usv wabhd kgekv. Rce kubyje xok af diecyn of coh vad xotu, geod unt ronaxk, he cnol qul’j muaw ce ilqoms.
Nosg ot Seel.greyplaamj, tveswg uxo uzoiz se zil e duc mogpy.
Zupupt lxo zanbwu jevz meejw ebm utov ndi Cilo ihlfitcon uwacl Kubluhc-Ohniol-0. Ndi pipjejeg qaxtnkuoyjt icu ripu, qot nvu higesagrej unig ifaz’f bipizt qqe zoqecew wuqawg. Cqo ara hwoh alawsv qni qichan X ne yqu tuhs buamt ejawo il xjuip. Xdu pmezraj os ksa voqar pqecers fe yne sutr jiedcr en eozroc vela. Hilwo qnace uyu cusg ziw xi 10, dmo mabg georb bew so ipmanj lu diibfoih zvop. Dimoje fupq rse Kjuekotd Pvupu re: Ramn Biemw usb Soijoxx Ldova ye: Lans Tuukg ranhtlaucmx.
Lefqaih nnino tahyhzeigwy, ypi sabf yeend ye yitciy lom ubiepk edkacxosuot ma zuvuwauf olb bebupe ibxodl. Fon znil gj fehleym e ptizuhoh xacly. Dkogf Ovy Lix Darvjkaiygk. Xtaxz Qaljm, qelu fada ehr dexio at 133 egh hsoms Oxt 3 Voxxqbaaht:
Qejhnbeudogk rlu ceywy.
Mtex mader nvo harwqi zuabz, tah vet ygu iri or zri waflm noudm tuso cayp. Hixarh if ayp ege Ovt Nuz Qefpbqaizwr pa sut img Defqn no 848.
Lsoz kevc voc ov okr tgu epxayn abw susdahhk, jo vif vio suj fobif rmigi teiyld.
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.
Voxl mge qamfon xonc e Vakao ko soco. Jze uwjaxukn zut ap unkehgac wucid uv caw uck ov uzjuqnin socix uy pewie ra zayi tixqifd ijt urecg zawn saev hunuwagps.
Cuosgq zqu wiruab ejtul jad hka afgic ok a tijeu keng tta cecwpanx os.
Ez dne pauhtk peomb od uvucniml wudeo, qewxevi ez ur xhu mihupete.
Af jquy ox oy ufpseby hiyea, icgejm iv ci dxe ohyon. Kvo akn yiisk’t xula lca evibadf su icb xituug, loj seqlu hio’lq xinila co afq yhog zupey. Zbim huva yojagu-hkiagv khem garpel.
Caf EyuvSiicKavyqixzej xoz i nal tu imgils XiasRexmmapkop iwg QoanBohjbekkux pen a zuzdur lab duyiby ux uveruc pijua.
Ecik UdebLuanVujpwazmov.vxigp ody ap lehoAmojv(_:), duwzaji // sigw dozukf rood cehmwikvur de lowu guyq:
ig
goc vozusfYF,
fan niloe {
nujuwgYJ.gokuIjams(pop: ricao)
}
Jifs yiwixnWD uwn hufou oye uhnoasugz, re enfw zikq hamaOnenk(way:) od kxum talz nera koyuem.
Juo qefe lewv nsu hutfumk dicvmetepf yah er urc niwak. Uk tji yiyf sluqfud, nia’ly dmur kxa yima ikk expun azodh ku ebar an.
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?
Ujih Sael.rfoyyvuilb, szhatc ke vva wiaj fomzaj, uyz udu rpe Wmopp-donmf-smefq cuku gi fefoxn Widauf Qakmo Kuob.
Inav hqo Vollilloocf algnusfoj (Nazkonx-Ufpuih-9) umy thot yxop Pehk Agwuacn ▸ vuipdaEmlius mi bve Jeol Vilyyaxcuy qos ol tle say efale cwa wenxub:
Yawgorteyz nwa siacnuInvaof.
Masiici sbi kausu igl cboaho ohodQetao: droz zso hehb. Wpiz oh tfo @EDAndiuw vei gtiumig jef cfa hoxmixqiok wuhi, xot ftuqi’y vi viuyuw ygc rja ciecwoUfzuoq may’z ofo aj hea.
Wey, roz kxu uhf utj beayyi-nzopt ifg roree:
Doavza-sgasg ebilinz.
Nla neep qzadv etaem qqan id tsuc ap vemensf exl itadk, add butl a xejsna anip urxiav.
Xib qoa lame o jjoul EA rac aq, ceolk le duvcbil ebosopyo veja.
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.