Relative Content

Tag Archive for swiftuiswiftui-navigationlinkswiftui-tabview

SwiftUI and NavigationLink – optional var in destination view is not initialising

I use in iOS app NavigationLink in NavigationView (for supporting iOS 15). NavigationView contains list of some items. I would like to use the same view for adding new items and editing exist (after click on NavigationLink). So I need to pass “PersonEntity” to PersonAddOrEditView. This view has 2 init (one with empty params for “add form” and the second one with “PersonEntity” parameter for initialising form fields).
The problem is that I can’t initialise optional fields (vars). They are staying nil and picker (or selectable list in comment) always “choosing” first value(1). It works if I make @State var age: Int? non-optional.
Also if I make @State var name: String as Optional than TextField will be empty (not initialised) for “edit” form.

SwiftUI and NavigationLink – optional var is not initialising

I use in iOS app NavigationLink in NavigationView (for supporting iOS 15). NavigationView contains list of some items. I would like to use the same view for adding new items and editing exist (after click on NavigationLink). So I need to pass “PersonEntity” to PersonAddOrEditView. This view has 2 init (one with empty params for “add form” and the second one with “PersonEntity” parameter for initialising form fields).
The problem is that I can’t initialise optional fields (vars). They are staying nil and picker (or selectable list in comment) always “choosing” first value(1). It works if I make @State var age: Int? non-optional.