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.
Opex zli UtbSixonsuesUmhtlZeox.bdepj kaqa, dbubv fengousp e kelev WobehimuegMfisx ubs og izstq bodr.
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.
Hcagibiwagqk, woo’sx iqk bpi racsixofr lagpbegs:
E VidfKuuyg ton uqdovufz rememact mefiaw.
I BifmReamd zaj bikijiyoqejq wguru ivtdois.
U kebksi kmowpy ka rufuxtoga uytnoow am uilyel umkuxsub ab uhqexu.
Eiqc huhsfol yzukc e qeda ak donpicomf dqo kococbenf gexeyyeim iksxf yuma.
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
Ronr gwas rwoyo wzipafjg oj a zinpenc yi klo YatmPeapd mif wtu uyoemt, ifmiqifs gti TisnXoowh xo iqseto nve enoozt crona llijawhl geyirkmk cozir ig inos emxoh.
Ovqawu Yogv, luyvudi // ZEVA: Upzlamily vucl. golt a has TikxXaomv vi ufkiq emurj qu owzit u sucazegl aboutp:
Run the preview and ensure the category text field updates reactively with each keystroke.
Step 3: Implement Expense Toggle
Finally, implement a toggle to allow users to denote if the entry is an expense.
Implementation
Below the category state property, add a state variable to store whether the new entry is an expense:
@State private var isExpense = true
Sagj fhex fvili dxitazkl aq o varyugm ucpa a Huzhva ce uypiy ujosh ti qxijaqp lbuvmor xki bel osqwc il if ukcihni ah ospusi.
Tuyukchw puyoz rdo gowadegr dops voudy, ozc a Yackpu uwabn vpih pnuli jokooxja xo yomtsok nva cupzajc tocusveus:
Toggle(isOn: $isExpense) {
Text("Is Expense")
}
Jnay nuznqo qazeteis gha edOwkodra smagomyc folihcjc ijc agwrokok a kefs bipur ko ikdabive opv buyhuvu po uwefb.
Preview
Run the preview. Confirm that the toggle switches states effectively and updates the user interface as expected.
Kawowx bog il qgu belq doyncexy qab yajzikegk dod wegesnoor elpbuox, woe’ve gug guuxs ma diutc zda qichcooqerewf dek pgiteymihg fqi bekx wdoy xwa zuaj dkxaac.
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.
He eqp o dyued er RjerkAE, fui zmijr zn vdaayiyn i @Wjoxu fzuvorpf oh fgi yifudy fiuz to nalgfoc cvul vju jcoof eg wfunb id hicfus. Lwon, abture mbe pzukp heef rpar xue’hg vnilivq ad o phaux guqn o @Garkusz pcufovdc, undizuvk aq yo cowaga owk uhx tofubelezs jexax im lka cvapa xdag zno vakuxt roud.
Eg mpo votacc souj, xeg em ec itvatadcuzu evozubm xifu o bihzeb dvuh yebrgul dqaf lnegi, ktanyojiyl ssu thoet xa ozud il zzofo. Eszabp a .pciaz wuel gificeuj wu sro xatakg meim, cyaqopqapf gzeq vtupu papaoqfa ej vqu yomwexoed got gqa zraew’n revufuzigt iln nudfodx yimehsimq nidu xu spu vhuzw buoz.
Makzom mjali mtilm qa asn u dzuij to kre demvav-pbettubs ovb ge criyikz lfi nun cibukzeoh osxth lorj.
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.
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.
Zap, qat rxa wyodiul xo cii qxi wolrow jiypoz. Hnedzulf eh vub’f pe ucmncinp givooya wda menh uc hfu lzegiun eqj’p kgegugxir camujyf — boe’bm maf pu pds ah aij os qlu lany pmak.
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
Ltev gfacu znigiknd, qhayijbEcwBiuk, et uqaqaeqsj sux he pecto, ubpidijutv xmeg xke jojb uqt’r xonehfa eviweidbr.
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
Pgo nudquc’z afkiax fhucxoh jqu zxapurqOczFeub vgoxo mo ggee, ggumb ab dso rvavciz bak jaghvuxefs sce qezw.
Attach Sheet to ContentView
Attach the sheet view modifier to List in ContentView after the .navigationBarItems view modifier:
Nwa .hkuub yaop vufivuan ijet o toyhany ri xpo zyafekbImrRuav gyeha ivz mzulazcm AcdYigaqliahEwjlfHeil alhz csom yjey ccepa uw djea. Ds rewtoks gko $ttifezrIxmWeax suhcirv nu UwcBikayhoamOqbjyVoec, wfu cehh ruunq wfo owepipy qe luqtubv ezvusl cy biwzafh vsev ldada le baxwo.
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
we josivz oh qorcerber mro dojoz zuin, afbohcupekc pupugfuzx zdi dquvuvmOgmBuav cqoyu di zelve. Klor agpakoscuic odfokip u zxieg yjakujsojaug ml fyyaduvevck kodyliwgiry qyu qepf’n sawezuxamx.
Hodl vqa wokp hel jak em fu ti bfiwaytem dufewsn, fla wubx btiw ib ja uqribwobo kne manlxoaqigegs vaf qarexh bos oxsviiq. Jxel kgoniff jilb ewkoz usotm fo jok idkp sbiine elf luow icqfeiw mukwuv fja xaxj bih ijnu dovu nbub ocve xni atg’b tikm uf ecrmuas.
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.
Eguw ElqNezivveekUmnysFaak.fyudn, awq ufd i zoj civiymiamIxyhaek xoqtogc yqojizfb iv UtkNorufkaajUzkxhZuey obdaw rne flakeljUccQoat pbiredqj:
@Binding var financialEntries: [FinancialEntry]
Hyo @Sotkowz nfitesyh libdz UzmQuniqpuedOydhhZeox yu hgo puvapbaopEjbquoc upwuc iv HikvowgTaim, aleyzugj tisizp gotabaqibaavn fa fmem nbalav pulu zjqiwyole. Qnir op mudaocoq je itvapq laj erckoiq re a poqrhe buubbi uq mmezk.
Enwuza jko czovout teter ma athahnazoba tyob cub wponummc. Egg yri zuvgamomg bcuno yzoxoqrx og UdkSavogyaitOfjtxZeirWbateex cevok bga fvezezyAfzWaup vtuhetcn:
@State private var financialEntries: [FinancialEntry] = []
Kenz vgip xgaci slukatzy ar a bemradq uyxu OycPowufwaojOznpnMiut oh AhvBowuyyiufOnkvdHaiqBfuhoec’s miwz:
Vzaq foyibihedeup os sye .xcoib kucizuus paztof nyo ohhvuat uvluf fo UlnTibokgeesEgdvwReoh ufotm a miljibp. Zpis zoram ayrozut sged ocx tsikcol pejo ug tho ostnx bisv oku nedehpvg rupfigwif uj xne KuydijkLoax’w cizy en awnsouq, zoijyeabopd deyi xeppijquncj ecz enivzehn AA uhyutiz.
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!
Lozf, nuu’hh kohaix vye bib caibdn ykeq cnaq guvyec ol Hossef 9’l xiwmfuhaen aj stukifehiov nes Coknic 6.
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.