This is a sample to display the picker in SwiftUI. In case of multiple lines, click here. Creating a multi-line picker in SwiftUI
Reference: Is there a way to call a function when a SwiftUI Picker selection changes?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
var pokemons = ["Snorlax", "Pikachu", "Slowpoke", "Meowth"] | |
@State private var selectedPokemon = 0 | |
var body: some View { | |
Picker("Pokemon", selection: $selectedPokemon) { | |
ForEach(0 ..< pokemons.count) { | |
Text(self.pokemons[$0]) | |
} | |
}.pickerStyle(WheelPickerStyle()) | |
.onReceive([self.selectedPokemon].publisher.first()) { (value) in | |
print("selectedPokemon: \(value)") | |
print(self.pokemons[value]) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |