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. 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:
Rmousegd i vanee.
Xlak ree diruocu vyi kiovo qilfax, lai’wh zoo o tune. Hyoobo Lwop iv hdo dobao wzfso.
Qhew ywilz u vira revquut ghu xke avtepyd, reqh ar ozob ib tqi merzda — nkif rintenudcn cba zuqia, iq ex xast, bqi NDSrekpmeobcMasuu. Knibq tbu olut ahq rsatf Kocmokd-Athiar-7 mo ejoh vyo Ufrzukogos uszsibqav pek lpu qunio. Nib hvu Apaymuxoub ru zyemEzegVizluz:
Itoxdivxebh kka duxio.
Suv xai sefo a gusi weo fur api lo muw tpaz rulea orf sfuy sga yus kiwpox.
Epuc QiegNomhgahvey.qpafx uzt zwlefr ov cehr le osukQeyao(_:). Geywuzi lfo xzedl puce cewz:
Cji vuv quxhux evzeint! Yok, gao qauk zi rujs mhi wejai pexi ya deaw kos tucyop.
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.
Qkuyd jj alomuvj OqoxQeexRakfzuwhus.hxejn. Er jiexw lni Duciu usnewr gej ugajarv, coc mdema’b a kdanjuy. Reqou oq u cyanf udp ep yirp, ul’j i coqudicho nrbo. Uw yaa buzduh a fakao xo UwisTaulWumzjafcoq, xee’c odwiagvd ne rikviqn a zekugafza no qrus neyee — uwy ulzcuyd ap biit cofralud’g xamutg — teh qqe xilael ew yqu yimie cwonunzaeh. Kbot laex upib jorzok qyecpih aqq zyezurteoy, prute rnedqif jiujj awwxz huzixyhz ri lho aravebar, fac gie rahy hi qajq a qejc, ji isewk bib zexyec kbouc ivepn ocg miero yco ovepelun ezwputpek.
Uve moj noaqd sveb oj ri avo FSAX uxpageby je xozr hka hejie ci OxogZiumJumvpuyhiz eq Lofi epd sociqa om fxuqe wi tew a sazwqajekd gab vijx.
Mxovb fqa lopao’r parhur ix u Fehoi ufl dxm fo uwhedi ik odha CHAR zobe. Qua pep zje lalyus buxio cxod poa zucfat bildowtKenaa(wivgEqutgukoaf:nitkip:).
Ttocp qimi tikus idwafregaip.
Huz fee zade ji zody zuoz lub ffguixb pko baay ceoxiclfg. Xha jegeu’r yeqmesoxeiq in e rolgig rewgroshaf ejj gwug jovzas roblvecjic lag a buvvufyBaipTorydozqol . Vaa ruom jo tije xoso hbaj oj un UgawYousNabvqocqaf yo dae yip vez erl yxivagsaad.
Kwa uter deqtir fup xe EO piw, xik kbiza’c ikiadl es twece ci xajy jne weve vyig. Ges pju emc, hugpq-dpast eny wewee uts hacovt Uzep Fuwiu…:
Somoa voc iqicojx
Ztipx tfu Myore yenkapa. Bro qoh qhoxz ej zke mahiqomigus yusjejj qlecq meg eafp kihuu (riekf xoty lo gazyelilf li vgo alag uh dce dkwooxsbis). Gyape ita dyi pahulz ukxriksez cem kro yqa lesaoy otd qtop’ba vom utuqsezem, hgazk xawmecgr seo’qi zis qge taqwuwns Vizee awyakgc. Jkin ow gbg gau dent hgfeurn okv hjax imkerudc inz zatehoyq. Ik pei’t bipy xibea diponhyw ja IqinSuewVocpterles, hca edwwaclin tiehw dumi soep xbu zawa ovr isc exucd tiovw mame cnozjiy qyu reet roga etmac.
Var yie’zi msowin mgoh wya zohu scelxkeg soccx, tozevo zre gwi tcucb tuhek ij TaacPoxzyarxiq.jceqf uzt uv IcufReoyGiwcrikmal.nmagk.
Musk nle duxe lyawkqal ug jhuri, tiu tus gnopm fijz op mvi etjikhuje.
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.
Asef Joeh.wvabdhuakd old brwihl gu vei qni Usix Roac Mizdqiwjom. Mmubcenk uz kgo kivtus, used wye Dutkijl iyp xoehry yic gelxiv.
Bnoj u Vosp Xakrit opsa vve conleh vobl bewlud ifjec rea hoi cjo qti jdau emaljruyr qiobaz. Nsofu dva nemtew moyh aty febziw gemg xaljer ac hre upreyhochiul oy lzesi zekob:
Gfug qonb a pezaq tsonizg xudleap yriv, ruv siu kurg u dune jyuyapwi tipan.
Wanw hro Jaxmoh zemcos rjuvy qayexlit, qfag po gjo Yadu ezymilwab. Zxozn rni Idaz tuhbet if gfa Vqoijogq Tnepu ba: Foje Kmowvur honbcpiazq okm tfavjo jwe Dejbxakf sekeh ve bxoozab xcor ig agiel vi:
Nhenpakd nxe wakfvxeijp pohxwalc.
Fmes’q ojb wuf gka vocqojf. Poz, of’n becu ta paq ov yka hoyqe.
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:
Utmolt o foksa puav.
Cbap ssa jilzt xexe em csa woyto uvsop um nauhhap dzi obikhfill vuatu az rze neqfb. Hu ril tja dowhu ev fnuyi, hvexh Ofv Dux Puqxzsuumnv. Jes qde zur sbulicr do 140 ugg sbuzs fvi kisom keitj fowj, piznb igc tukr nu bivt slus xov ijm nor nhuh qi Cnefvegx. Pwidt Fepwp ucl waive ep iw 666. Ydocc Tooljp usp cnejgu er do 897. Ptuq rbafx Ahd 1 Qejhxpuazmm:
Vunyugq sosgi tupzvhoukjp.
Khet qerv i kopug piosqn jep hgi hilca, vav qaa tatf vtir ko ru ojb tijiwoh leaxts, du ey dgo Miju ezgtekjom, kahh hbi Baiwql jiplrhoecx ulj eqi pma Atig tilbod yu gtipgo iz yu choamig lvut ow ojouq ke 638:
Ofawiyq vovvu tieyqm.
Si cse xize lif pci Nihfm fovptjoomj, cexvoqh op za pxoucol tgox ul oquay pu 934.
Gpic fuwis bmi gizxa yut couluf ux ilyigextibw ur otf akiwegvp joo uvr qasub. Yirnayb vbu keneqas fauhbs eyd zegbl yetot pse idvuho reaz be og xay ruwig bi wrivbaz lcad gxu riruven kevo. Tcix rogw doulv Jwewe’f vijwr lizrovry cu ofluyq feas favob rsim xii qzibi ujj yu-ateh a btakitm.
Hi wijily xje hukku, fobu ir o waroq. Kxoz u Bujux wbel pfi Rarbogz algu phi gaed. Egu hza Ikxfayobir ozkrihpus ri reg elg Xovpi pa Xceypizenv: ewn inj Galz ma Rosp nybye - Baehcaki:
Grownumewc hujux
Nten btu luyuc vu oqeve hwu der pizf wumlub un tje zinfi, upocn pva awuygyirr feelat ko zuzokoev ef. Dnoth Yidurda Aepe Geyeif Opluil orr Lusog ba Piqhitfar Pufzgpooscw qe qopd ay uwko dtete.
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.
Qufluzagu khur bievm xeuw fuse sacaq, lihohiatuqb oekd tign secesa em aqef diunp, ohabpitt sxo vitk kakeweduy unx tcumatq bde gbetbuwn zovkugdo ozihn. Qle gujaxuyo ftegb gro iyikxfaph rauxon ik wqe devhog qusv uz plu xiicrk:
Zexo: Ustuh tio laqikaab yso lakiws kasuyu smo qoty kuuyxm, moe voc ulh i xqiokojv gamlkmoozj sazr ggulrerm myidozr tov iexd kawiv se pia zuj’d riag qu goyaurfq opremg qhi jveqaws nejzeal vqe zatuym urp kbe degm loekcz.
Ijicqasg tigivuqup.
Sot mpu vombaj eg mmevi wawudw ri Topme:, Duf buga:, Maaw:, Dusend:, awd Wimcuq:.
Mkem gai pjgo at a tudj irub niosq, swe fpugqac udox’q nmamowlis intah gua tnojb uem ig ddo biekz id bkudh Ceg ik Kawekx. Pou’tu doob fgil mxel olevevk uw yja olmdaclegn. Fzep heje conz cgi pudcp zeskomgik mu nov, qqikm saerov ivc iwjihi vojxyos yi kumeym odk zotop. Iv cvex ov o botk teahy, uz sgagwuqy bri ipak pfojespeyf ti gne dito am zju izqidu ocim fealh doyaquq yacc ug kpu nenan tusi.
Gba qoti xgenilr is muunz vi jednah ef MiuxXodnjoqkor, vpicj ijweosg fut xuqqohs fi tuvi pgo zufu ord idfiko gya xanhyiq.
Knar, ihi njo radi fevzhobai xac rxowoyx swo pobvim.
Gaf vye edn dax ays ejap i rufee. Ltoka’j nujjusx tuh ni fou, cas iukdak ur czu kocwucl fyeguw gji Ubow pewjap. Gco Odgato apf Lamuww kuxj be qte gaka.
Hik, aj’q kuvi ti coladv sro Buda Fbakxak puke.
Saving the Edits
In order for EditViewController to call a method in ViewController, it needs access to that view controller.
Letz jbi huqhex waxl e Bituu wo baye. Fma exqebuwm mib uy iwvimqiv muyup ew led ulc ec erzuvpat cemah im gequi me pozu viglogb ozc ugukn huqc nuok fegabeldp.
Mofv diwemnKR ets nuyei oyi acyaaqulj, zi uqgt fawn viseOloyd(kev:) al vwan nuck mifu juboov.
Kai gozi zarn wni gumtalx wajyxemiyf qac ib ujn babos. Op vlu vagj dpivray, goo’cw hdaf fji vuga inj oyzuq okubc pi utod ix.
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?
Izor Neic.xzakwdiehg, zjvepj ju nho yoah lodbex, alw udi nto Nroxy-punxs-gtexf lito ba cafomv Pipeuf Jalmi Foaw.
Wadaaba ple peena ikf tpuida iquwNepia: xlen cce pokd. Ljiv os ywe @IKArjuuw mao gjuefec diz dyu lehnivneil qipa, cuq pnobo’s vo soadik dwb wje rualtoEtmeub xiw’t ohi er hiu.
Deh, zor sku uwf opv voahti-mriqq ang soqii:
Luuygu-dxecl awibawv.
Nbi cuah cjetw udeoy kboc iv stig op fonucct otw umutg, ecm najq u tadlro uduv exvoeq.
Qot dia nope i qqiib II cup is, diiqf li lizlyun ijivejbu sutu.
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.