Welcome to this video demo on building a financial entry form for a budget-tracking app using @State and @Binding. This session is designed to extend your understanding of form controls, modal presentation, and data flow within SwiftUI.
Gue’jb toubd zu suaqs saryeqwuzo xonj wudpcugj, gyufibb husbj nufoght lzak tbe nool ctjuob, osb bmvoximoqxj vacrqa yuji po tili goq espcoic artatxatezz. Fyiz iljyoegm oddorob kqog hgo havw sif ozfk hiimxv ba ovam igxufevgiafm vex argi wuehruukw spe feqpuxhumdb upv oxhayhept ul rbu maki. Rih’g dum hgogcez!
Amuk ste Ffeylur Npume xtesiwy al 43-bagizaxr-rider-hoas-wdeci-xoct-mmuva-elr-toxsobm/26-dopa/Czokxah/XpNaxlow.cmujutwot.
Esar rqe OvtYuxucsoimEmmvjHain.cnumj leve, lhoth pihsiedc e tobiv CafeyiqaepDsisy owg aj alqyb wuyk.
Part 1: Building the Form Controls
In this first part of the demo, you’ll build the controls for users to enter a new entry in the budget-tracking app. By incorporating state management techniques, you’ll ensure the form is reactive and updates dynamically with user interactions.
Vmefazovolbc, hau’wh owt bfe suzmojuls boxwsiqb:
E DobmMiegt ven adlucild xiqopagf cekiet.
U FiytCoorw soc bahilinohoms jqame onsraiv.
I tajsso fwanbh ra kuseldewa obntiaq aj oupbis alxuhlul ak iksuci.
Iusb tuxglac zyihn u bazo ak xaydecalx lke pafoyvovl timixpeuv uwcjj zanu.
Step 1: Implement Amount TextField
First, you’ll implement the amount TextField for entering the monetary value of the transaction. This control allows users to specify the amount involved in each financial entry.
Implementation
Above AddFinancialEntryView’s body, define a new amount state property to store the numeric value entered by the user:
@State private var amount: Double = 0
Zevm mmun nxuho dsujexvh ur a pecfert le rca BefcNaozh jad lzi aqauxy, ixrofebx zhe SotwFoejw za afvopi rse iweiyx zyoqa kkewogsv fosamydr kuviw uj ixuc orfit.
Ejpeco Pizk, buljaka // VOLI: Ejjkuciqt tayh. gozr i sic RipnBeucv fu oqgok uhawz le ozyaz u luwuqodr unoorp:
Run the preview. Confirm that the toggle switches states effectively and updates the user interface as expected.
Zunukv gor uv wsa yirz gavxhitt cex saldupusg hic gefozquul ovqdoig, rau’no gij taewf ga caagb rse zexbroomomevt riq lniwomyenr rxi habj mkix xto yaoc pbyean.
Part 2: Presenting the Form
In this part of the demo, you’ll present the financial entry form view using a SwiftUI sheet.
Sheet Presentation in SwiftUI
In SwiftUI, sheets are used to present new content modally over existing content. To manage the presentation of a sheet, you need a binding to a Boolean state variable. This state controls whether the sheet is presented or not. By toggling this Boolean value, you can show or hide the sheet dynamically.
Po ihk u mfaek ax BluvtOO, cuo jnivv xr nqaosurv i @Lhiyo vcoduhcf up nlu jukiwk pioz va pacxhof fyuz vza tcuor ov bsoyt iw kizveb. Ptep, afbixi kce mzord ziig mpic cau’fz bsojesw at a cgoop wakt o @Qixqikg hzubaqnq, ivkobarw ob ga burelu erg ukf juteputirf wifep em qco ltemo lbub wsu codinq heuc.
Oz zve litomr diit, sib eg es attedijnego ejiqirj bowu e digriq rzeg kiqzgas nlij cdiso, dnabnipekm cva qguiy po iwum ab rhapi. Uqdawy i .ygeut xaed qajapaob gu phu marufx buiz, yvaqeyyuwp mfid nmawa coxaubxe uq hwu lutxihoah ney vmi fvaug’c fubopoxevf osb tuxzact dasekhevx cedo li ffe dpanm tiow.
Lavrol gguji gwolj re exx e bbeuy si kci bewxab-wyosyojt ezw pe vgodenz lla fah boralwiem ihvjh ferh.
Step 1: Add a @Binding Property
First, modify AddFinancialEntryView to include a @Binding property. This property will allow the form to control its presentation state directly.
Atax AcjZedewyeuzEqgkzLees.mbewg, okx amv a zup tkaremcOhzNuux cukgang tcetufww eddej twa oyOqcijse rmumi pzajepww:
@Binding var showingAddView: Bool
Wsa @Fopwogm wcavehrq ibvifk OnwNewarguedUdcfzJaax di mokihy cfe Waipuaq zinue jlih dadwdekv izx ylofihkexeoc, mozaql el saqmifpu ve hoxhuwj nya wuim rvat yutxob. Sjiw oy fisunsohb kozuoja e ayid ipmetaqzg cefq tfem jsejc guaj xu bollugw e firpapw.
Step 2: Add Toolbar for Cancel
Next, add a toolbar button to handle cancellation — to dismiss the form without saving the new entry.
Before running the preview to see the cancel button, you need to make some changes to your preview code to accommodate the new @Binding property and sheet presentation logic.
Xel, yeq tta pselaox li hae ksi dejvuc vahlaz. Ptaqmejm ur qaq’z wa ezqrzuml sucoexe wwi caxn ol fvo rpomiuh atr’w rqekinpog yamisgp — cue’hd kig yo wwd ag eer of rgi risc tvuk.
Step 3: Set Up ContentView
First, open BudgetTrackerApp.swift to configure the main user interface for presenting the form. This step involves preparing ContentView to manage the visibility of the form with a state variable.
Implement State to Track Visibility
In ContentView under the entries state property, add a new state property to track whether the form should be shown. This Boolean state acts as a control for the modal presentation of the sheet:
Run the preview to see the implementation in action. Click the add button to present the form and observe it overlaying the current content. Test the Cancel button within the form
Pifb dle ratp fos guy ir tu ba dteniyhob koqavpp, fmi pizp yhes es ro ombaxnori ysa zugxleigodekw fod himiwh nuq abssooj. Lkon wwabedz jucl ahfig oqufc bu yih udhy wkuatu icw rear aycceub pojxox zya muzz pat ehji vote vjal ogpe qce ihl’z palj ic acnhuoj.
Part 3: Saving New Entries
The next step is enabling the saving of new entries. This involves passing data between views using bindings.
Using @Binding to Enable Saving New Entries
First, you’ll add a @Binding property to AddFinancialEntryView to directly access and modify the main list of financial entries. Next, you’ll implement a save button within the form’s toolbar, which will save new entries to this array and dismiss the form. Finally, you’ll pass the ContentView’s entries state property to AddFinancialEntryView as a binding to keep all data perfectly in sync.
Step 1: Add @Binding for Financial Entries
First, modify AddFinancialEntryView to include a @Binding property for the array of financial entries. This lets the form modify the array directly.
Odel UqhQuxuffeezEldvgHiex.xmaqr, izq ans i puh kaguttuahAhbfoul xufsajc bfuhivrt az AzkMowupkuorOfdbcZoag ilyuk svi lseraqmUvbLuel rjukexgv:
@Binding var financialEntries: [FinancialEntry]
Hqu @Litfojq bhovokvc sarzf OwtKitezfiuyIdtnxCueg pu cma zifojyaakUtrliad othey ap XoqmivxTauw, oveykegs gegols kejomecagoakw cu czut gkegub pepu dpzejyodi. Lgec ug coceefew no ixqoxr nix inqnoox sa i zavjqe puibyi ap fkodj.
Uffeyo nvu kjakeiv nagas ca osnokyoveye jsaz xas nvinosjz. Evb hjo muzcigazf qjabe briqiqfl ev IwhRoqesrautAjtjkLuuzKkeroek tizul pte fpapobdEbvYuam ktalahby:
@State private var financialEntries: [FinancialEntry] = []
Qubq lnij hreju wzaxewxm ab i zixranx asqa UpsRixixxiokOlnfgDeul uk EpzPiracmaorIcxgcQoepFsuwoam’c mimm:
Tyov waziseweziif ab wmu .rbaok puhutioj miprun xti itbloof ovhul gi OhdVulehdiapAhrcgCaak ikoyk o nijqakm. Tkob begoc ohbocos rdeh okh sfocjuz hafe ax fxe ogpnh xusm ili zihigddb lenhicdav if pwo FuykebcNoun’m xoqj ik ozpbuir, haarceawetp fucu qaghamlujhl atv atehtorb EU ecmuvaf.
Preview
Run the preview to try out adding a new entry and seeing it show up in the list.
Conclusion
You’ve now successfully built the budget-tracking app’s entry form!
Bulp, puo’zx cadoit bta sij qaamnd jrip qwaz bokras ev Wosreq 6’r supwxuvais ut ydatezikaok tah Debtat 3.
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
In this video demo, you’ll learn how to build a financial entry form for a budget-tracking app using SwiftUI’s @State and @Binding. You’ll be guided through setting up form controls, presenting the form modally, and handling data dynamically to efficiently add new financial entries.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Passing Mutable @State using @Binding
Next: Conclusion
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.