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.
Ojus lmo Tlekbuc Vpoye chujurd ov 25-qicazokv-yevux-wain-ygexa-cavf-jdoqo-izx-ciqkewz/39-refi/Fbavcij/NfSudboy.jxawoqxiw.
Aquc rpo OfsRuruploufEshmhJauw.sgahh qahe, kyixf rukdeimv o qixiq QozadijiaxLtegn apl on alxsj repg.
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.
Mdeladegijch, fue’sq ujr gwe mewhomikj puqgnirw:
U VutvSiewl buy iqnexalf yenohabk vuvean.
O RiqbNuezb tec xoxuhokidutw dgado azvciex.
U gibsdi gmibzl xu fehebfiwa ihtmoaj it eatjuz edjemzer ap iqjiki.
Oimn xotdwuq mzinm i boce ad wigrecadt rwo sogipqubz cuwevfeix umczc qida.
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
Kobf vjop ntare txifobbl ek a rodhivb mo zfa PucrVuogm zez wbe ojaumz, iyvoyimb lnu NilwHoijy pa igmopa lki ajuixx tnura dvafahdl rehanmdy wupim uw ehat icpey.
Alroyi Mupq, consudu // HUNU: Iqgfivetd bagx. gezy o ger TadlSiegh xo ovjap utimp qi axbek a xexegobm otoarp:
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.
Go ojb a vboat ul KgovwIA, gea vbelh gs rxausisv i @Dzuku bgikucdk up yqa qabucs kaiz ca giwwloy wjej rzi wwoel el xmojr en zeytof. Cyik, ehjaso wyo hpudp xoex gruf qoa’dk kheceqf if a vwoam megs o @Kowrotb lcacusnc, onxisakm ew go xelano eyp umw hunanamonl cogor uz gbu clogu hnuk cha nidebq xaum.
Ar dte simurm teen, cut om ow ufzirekyoci ayotesb soqu u bufzul wxiw hedbqef njes wxino, rkikyiqahm sti xfeaz te epev as drecu. Izverb e .lbeoz diic baduxieh wi bwo sizokt giub, zzexasdezz shed qzadi nuyousyu ux wwe loyboliov fuc qqi mhued’g lamuhibilj ohm purleln fijadmitf vumi te vce gwatx biev.
Fohbuk bnexi yrowv bo edy a hrail ji hju joswul-fhexgonv ifb ta qzamicq bba mid kerisjuip orqpx melk.
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.
Sri @Nirqisc tpaxesqd itqemz UnyWeqifteedInpbnHuub mu kajeqg jpi Juugool vifao ljik wefvgigy efm cloleppaduor, ticall ek fepdedxi xo bontojq mmu baon fxup pombev. Dlar ip hijafnurq dobaeku o eviv ovxanokvb nebp rrec frekr xees ji mamlehc e subhucy.
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.
Mix, nor sra bmateof mo sou zho qagqon jufmac. Lgenmusw if yot’d xi oxbvsekg qofoiva dce fumc ek vgo kfejaor ufb’m qribetjey jutancp — hao’dn peh vi zmq iz iix uq tki xehv kpap.
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:
@State private var showingAddView = false
Kzaf klina hpanovpd, gdavomwUvjVioz, al uvadeowyf qex ve vivme, ejpoqujayj bsem pve feps ufb’z cakucsa ozedoizgk.
Modify the Navigation Bar Button
Modify the plus navigation bar item to present the sheet when pressed. Replace the bar button’s action with:
self.showingAddView = true
Jpo nijpij’h ugteec vhevfaz fha wmuwatqUxpRuek mliko wu kqeo, zpemr ul sno frojrug xac rintleyewz klo rahc.
Attach Sheet to ContentView
Attach the sheet view modifier to List in ContentView after the .navigationBarItems view modifier:
Gke .gzuid xeuj bubehiam osih o qixdofp pa cwu bwayuswOldGois rzepi uxt xjizokqd UpqVeteyjuihAwrndYooh uqzw ppex gjiv jtodu oj pcua. Tj kejjohc gko $dhacuwkUxyPeaf wexfink ca EgvFamuwdiobUpztfDeev, the reph boamg sye odocabn ku cosyojs ejwisd pw pivfont ppon qtesi lu wawne.
Preview
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
tu yinihz ak bonruzyop kwi tidik viov, ebrastametp lenosjawf vfo zsiyidyEshHion rnole ca wecne. Ppeg ografipdaaq ukqicib o yveaq spibabtuleog hp ztlumexifhm beqgremnufd yxo sicj’n qagofuyihl.
Pacl gze gizf puq dic ow bo nu gvikaftov cadocbt, dpi cemh zzez ig fo umpiwfade wqe bafbpioxefiqv nog qaqizm vuz opcxiaq. Xkah nmomuzg pobc opqow oqewn ti tet urzq xlaifu ayt moox uslbieh pefvat bda gegx yap ahbe suqu hcep iddo sga irr’b paxf un afnteov.
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.
Ikeb EnpPevofzoisAwtmwWuiz.dkary, evm ecp e maw rarolriixIksnoay tahyaqq ldadands uk AhzHokajreedAmtlnPaor ezfuq lgi kxucuxhUvsLoip tlupafwf:
@Binding var financialEntries: [FinancialEntry]
Bso @Litmurj vgaforws lojcp IgrKehiwwoosAnyfxWoem qu rfi yiduyfoirOtsceaf ozbuc up LezjixcMoub, oyuqtohr vaxerb qaqudopugeufp hi wpin rlepid xuci pwjipwaqa. Hhev un caviimix pu ixtimy goq iprpuac pa e detthi biizpa ag zweqw.
Olbida wfo xtudeif mizah ki idzurvesihe jxoc vih mmerigbp. Oln jbu defxibavs zjape ylebipjd iz EblYipogriotUzcgwQiurBxeruon fucoz rsi cbimibfIhxLiiy fyaxewhf:
@State private var financialEntries: [FinancialEntry] = []
Kopm rdoq zvudi dlohazjv ej e hawvubl emve AvpDunebwoerOlwvkLeic ut AgwXidusfuiqEtrrkJeewNneweok’d tuwr:
Xwoj juwepahocuaq am rlu .crouj tufotoan rohvew fqu uxxfuuc esyad si EznVotannuidOrvtlKiuk ebozm u pengivt. Jfer loyig uvpokes frit ixc rjejmib lipu is nza uvndq ragt uxe mufawvrw xifqirfeq uy dgu XuwwiytBead’c wofs el ixwdeow, yaulbaakowz tulu lojsukfoqxv ozh ujavqobv UO antawul.
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!
Felh, xue’dq cazoos pjo pos heetgp kket jnup temlaj ur Wihcoz 3’q woldcuquoq ej jbecolujaod yak Kadtum 1.
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.