Creating the Picker view
To create the Picker
view, let’s make a new SwiftUI View file called PickerView
for this purpose.
Inside this file, we need to add a Binding
property so that we can use it inside ContentView
, as well as add an array of titles for the picker:
@Binding var selectedPickerIndex: Int @State var levels = ["Easy 😌", "Hard 😓", "Extreme! 🥵"]
The code here has a Binding
variable to hold the value of selectedPickerIndex
; that way, we can keep track of which button on the picker the user has selected. Then, we have a State
array of titles that we will use on the individual picker buttons, along with an appropriate emoji signifying the difficulty level.
Next, as we’ve done before, update the Previews
struct with some dummy data, to satisfy Xcode:
struct PickerView_Previews: PreviewProvider { ...