Gurmukh Singh
Gurmukh Singh

Reputation: 2017

Selecting a picker value to lead to a text field, SwiftUI

Im trying to implement a feature in my app.

When I click on my picker:

Picker(selection: $profileViewModel.education,
     label: Text("Education Level")) {
     ForEach(Education.levels, id: \.self) { level in
         Text(level).tag(level)
     }
}

This takes me to a screen and then I select the value (this is fine - it works as expected)

How could I select the value which then takes my to let's say another screen so I can fill in more details regarding the selected value.

For example the above picker has a values to select eduction level, after selecting it, how could I get an action sheet/another screen appear so I can have a text field there to save this extra data to or once the selection is made, a text field appears for me to save some extra data, and then clicking a button which would take me to the original screen of the picker (hope that makes sense)?

I've tried researching online for a problem similar to this but can't seem to find one/or if you can point me in the direction of what I should be looking into?.

Tried the following:

enter image description here

Upvotes: 0

Views: 1823

Answers (1)

Asperi
Asperi

Reputation: 257711

If I correctly understood your scenario here is a possible approach (replication tested with Xcode 12 / iOS 14)

Picker(selection: $profileViewModel.education,
     label: Text("Education Level")) {
     ForEach(Education.levels, id: \.self) { level in
         Text(level).tag(level)
     }
}
.onChange(of: profileViewModel.education) { _ in
    DispatchQueue.main.async {
        self.showSheet = true
    }
}
.sheet(isPresented: $showSheet) {
    // put here your text editor or anything
    Text("Editor for \(profileViewModel.education)")
}

Upvotes: 2

Related Questions