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

耗尽温柔 提交于 2021-02-11 17:55:20

问题


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:


回答1:


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)")
}


来源:https://stackoverflow.com/questions/64195692/selecting-a-picker-value-to-lead-to-a-text-field-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!