Dual Purpose Form and FocusState in SwiftUI

preview_player
Показать описание
In this video, we are going to take a look at a few things, but the primary goal is to develop a method for presenting a form view that will allow us to either create a new object or update an existing object of the same type.

We will be looking at creating a form along with a FormViewModel that will help us do that along with an enum that conforms to the SwiftUI view protocol so we can use it to present the form for the correct purpose, either an update or a new object

In addition, we will also be seeing how we can use keyPaths for properties in our ViewModel so that we can control the field focus in our form.

If you want to support my work, you can buy me a coffee :)

******** Download my StewartLynch YouTube Channel Application - Free ***********

Table of Contents:
0:00 Introduction
5:42 Preview Content
8:48 FormViewModel
11:12 FormView
16:05 FormType Enum
22:16 FocusState

Starter Project Files:

Completed Project:

References mentioned in video:
Nil Coalescing Blog

Other References:
Stewart Lynch's YouTube Channel:
Stewart Lynch on Twitter:
Рекомендации по теме
Комментарии
Автор

Clever stuff! I couldn't get the keyboard SF symbol to display, except by deleting the Spacer in FormView. Not sure why. Updated the code for the deprecated NavigationView for NavigationStack by changing the Minimum Deployment to iOS16.2. With these two small changes you have a new iOS 16 video!

brucknerdevilliers
Автор

I didn’t know that one can conditionally call a function in a button.
I’d defined two computed properties displaying a button an then conditionally displayed either one of them. Your solution is more compact. Love it.

mhfs
Автор

Perfect timing. I am currently working on a project with forms and I was trying to figure out the best way to display new and update views to modify my record. I will definitely use what I learned from your video in my project. Thanks Stewart!

miketurner
Автор

Another great video. thank you for putting effort to record such informative videos.

erdogancete
Автор

Great video Stewart, thanks for the clear instructions!

jordibruin
Автор

Very informative and useful tutorial. Can't wait to give it a workout.

kenturnbull
Автор

It's no longer necessary to delay to focus assignment on appear. You can now assign focus in onAppear immediately

SentimentalMo
Автор

Excellent video Stewart, thanks.
Question: in FormView, shouldn't 'viewModel' be an @ObservedObject, instead of a @StateObject, ? (Because the FormView does not instantiate the 'viewModel').
Cheers.

wmblemania
Автор

Ooh, me likey this generic solution :) Nice. Thx

paulberghege
Автор

Another awesome video! Stuart could you do a series on working with SwiftRealm and MVVM?

chrisstromberg
Автор

Another VERY fine video! The FormType selection is brilliant!
IntersIntersting with Xcode 14, a Spacer() in the HStack with the Keyboard dismiss button hides the butThe code Code still runs, but nothing on the screen. Remove the Spacer, and there it is! Weird.
I created a test project to replicate the behavior, something. It must be a bug.

Thanks Again!

holycrosscv
Автор

Very helpful. Somewhat related topic suggestion: app structure for separate view models per view and view model(s) for each model (store). Trying to move beyond a single view model for the whole app. Feel like your FormViewModel with injected Store is on the path but I don’t see the overall picture. Thanks again.

tsbob
Автор

I know that you made a good video about creating status bar menus and icon for macOS app in SwiftUI, like putting app icon in macOS title, but I cannot find that video! could you show the link of that video? thanks

omidrostami
Автор

Your videos have been incredibly helpful Stewart.
It looks like .onAppear with FocusState is working now, as I didn't need to set the delay to get it to work when adding a new contact.
I guess to make up for it, it looks like the spacer doesn't work to push the dismiss keyboard icon over, it looks like it's being pushed offscreen as it's not visible.

ColeDano