2 TextField with 2 picker in 1 UIView - ios

I made a 2 textField with 2 pickers in 1UIView but I can't show the right pickers in the right textfield because I don't know how recognize the current view active.
I tried something with tag (but only work with pickers, no with textfield), and create var current = UITextField()
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var picker = UIPickerView()
var currentTextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
GenderTextField.inputView = picker
WitchSportTextField.inputView = picker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTextField == GenderTextField{
return genre.count
}else{
return Sport.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTextField == GenderTextField {
GenderTextField.text = genre[row]
}else{
WitchSportTextField.text = Sport[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTextField == GenderTextField{
return genre[row]
}else{
return Sport[row]
}
}
Someone as an idea ?

here is the complete code
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var genrepicker = UIPickerView()
var Sportpicker = UIPickerView()
var genretTextField = UITextField()
var SportTextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
genrepicker.delegate = self
genrepicker.dataSource = self
Sportpicker.delegate = self
Sportpicker.dataSource = self
genretTextField.inputView = genrepicker
SportTextField.inputView = Sportpicker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == genrepicker{
return genre.count
}else{
return Sport.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == genrepicker {
GenderTextField.text = genre[row]
}else{
WitchSportTextField.text = Sport[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == genrepicker{
return genre[row]
}else{
return Sport[row]
}
}

Firstly assign your both TextField delegate to self in viewDidLoad
GenderTextField.delegate = self
WitchSportTextField.delegate = self
Use this extension for identified which TextField is active.
extension YourClass: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
currentTextField = textField
picker.reloadAllComponents() //Reload for accurate data
}
}
NOTE: Variable names should begin with lower case letter. Use descriptive names to clarify your intent:
Look at this: https://github.com/MLSDev/development-standards/blob/master/platform/ios/swift-coding-conventions.md

Related

UIPICKERVIEW with different data to be displayed according to the row selected

I have attached my code for your reference. I can able to display the data in Pickerview as i change the row the data changes accordingly but the data which selected from the picker is not getting displayed in the UITextfield. if I select any option in Pickerview I could not see any text inside the uitextfield if I select it still showing blank text field.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet weak var textfield1: UITextField!
#IBOutlet weak var textfield2: UITextField!
var x = 0
let leaguepickerview_destination = UIPickerView()
let clubpickerview_denomination = UIPickerView()
let league_destination = ["India","Europe","Australia","New Zealand","USA & Rest of the World"]
let club_denomination = [[" Rs.500","Rs.1000","Rs.1500","Rs.2000","Rs.2500","Rs.3000","Rs.3500","Rs.4000","Rs.4500","Rs.5000"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"]]
override func viewDidLoad() {
super.viewDidLoad()
leaguepickerview_destination.delegate = self
leaguepickerview_destination.dataSource = self
clubpickerview_denomination.delegate = self
clubpickerview_denomination.dataSource = self
textfield1.inputView = leaguepickerview_destination
textfield2.inputView = clubpickerview_denomination
textfield1.delegate = self
textfield2.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch (pickerView){
case leaguepickerview_destination:
return league_destination.count
case clubpickerview_denomination:
return club_denomination[x].count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch (pickerView){
case leaguepickerview_destination:
return league_destination[row]
case clubpickerview_denomination:
return club_denomination[x][row]
default:
return "an error occurred"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
}
}
}
You need to set the textfield text
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
textfield1.text = league_destination[row]
}
else {
textfield2.text = club_denomination[x][row]
}
}

How to use UIPickerView with TextField more than once?

Below is my code, this allows one Textfield to have a pop up pickerView as an input source. I want to know what i need to add to allow a second TextField to also have a pop up pickerView as a input source.
I have managed to get the firstNametextfield working, but havent managed to get the secondNames working.
class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var secondNameTextField: UITextField!
var firstNames = ["Lewis", "Jason","Alex","Mason"]
var secondNames = ["Davies", "Jones","Rees","Kristensen"]
var pickerView = UIPickerView()
override func viewDidLoad()
{
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
firstNameTextField.inputView = pickerView
firstNameTextField.textAlignment = .center
firstNameTextField.placeholder = "Select Name"
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return firstNames.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return firstNames[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
firstNameTextField.text = reports[row]
}
class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var secondNameTextField: UITextField!
var firstNames = ["Lewis", "Jason","Alex","Mason"]
var secondNames = ["Davies", "Jones","Rees","Kristensen"]
let firstNamePicker = UIPickerView()
let secondNamePicker = UIPickerView()
override func viewDidLoad()
{
super.viewDidLoad()
firstNamePicker.delegate = self
firstNamePicker.dataSource = self
secondNamePicker.delegate = self
secondNamePicker.dataSource = self
firstNameTextField.inputView = firstNamePicker
firstNameTextField.textAlignment = .center
firstNameTextField.placeholder = "Select First Name"
secondNameTextField.inputView = secondNamePicker
secondNameTextField.textAlignment = .center
secondNameTextField.placeholder = "Select Second Name"
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return pickerView == firstNamePicker ? firstNames.count : secondNames.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView == firstNamePicker {
firstNameTextField.text = firstNames[row]
} else {
secondNameTextField.text = secondNames[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerView == firstNamePicker ? firstNames[row] : secondNames[row]
}
}

Having multiple Picker Views

I am trying to have multiple picker views in one single view in swift.
So far I have created 2 different textfields and I want both of them to have different picker views if I click on them. so let us say if click on textfield #1 it opens the array #1 and textfields #2 the second.
I have already looked up these questions but they do not really answer my question or solve my problem:
Multiple Picker Views on a single view
Creating Multiple Dynamic Picker Views
so my View Controller looks like this:
how my viewController should look
and this is my piece of code:
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
weightTextField.text = pickOption[row]
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.inputView = pickerView
weightTextField.text = pickOption[0]
}
so as mentioned before, the first textfield opens up the array 'pickOption' and the second array opens up 'ageOption'
EDIT: 'weightTextField' ist the first textfield which opens up the first array(pickOption)
We can do it for multiple UITextField from single PickerView, with the help of UITextFieldDelegate
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
var currentTxtFldTag : Int = 10
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.tag = 10
ageTextField.tag = 20
weightTextField.delegate = self
ageTextField.delegate = self
weightTextField.inputView = pickerView
weightTextField.text = pickOption[0]
ageTextField.inputView = pickerView
ageTextField.text = ageOption[0]
// Do any additional setup after loading the view.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTxtFldTag == 10
{
return pickOption.count
}
else
{
return ageOption.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTxtFldTag == 10
{
return pickOption[row]
}
else
{
return ageOption[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTxtFldTag == 10
{
weightTextField.text = pickOption[row]
}
else
{
ageTextField.text = ageOption[row]
}
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 10 // WEIGHT OPTION
{
currentTxtFldTag = 10
}
else // AGE OPTION
{
currentTxtFldTag = 20
}
pickerView.reloadAllComponents()
return true
}
Output
Suppose that you have two pickerView controls: picker0 and picker1.
class HomeViewController: BasicViewController, UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
let numberArray = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180]
var areaArray = [String]()
#IBOutlet weak var picker0: UIPickerView!
#IBOutlet weak var picker1: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// area //
for i in 0..<11 {
let str = "Area "
let localStr = NSLocalizedString(str + String(i), comment: "")
areaArray.append(localStr)
}
}
// MARK: - Campaign duration & area
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == picker0 {
return numberArray.count
} else {
return areaArray.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == picker0 {
return String(numberArray[row]) + " pieces"
} else {
return areaArray[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == seventhPicker {
} else {
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.inputView = pickerView
}
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if weightTextField1.tag == 1 {
return pickOption.count
} else if weightTextField2.tag == itemPicker {
return ageOption.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if weightTextField1.tag == 1 {
return pickOption[row]
} else if weightTextField2.tag == 1 {
return ageOption[row]
}
return 0
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if weightTextField1.tag == 1 {
weightTextField1.text = pickOption[row]
} else if weightTextField2.tag == 1 {
weightTextField2.text = ageOption[row]
}
}
////set delegate
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
if textField == weightTextField1
{
weightTextField1.atg = 1;
weightTextField2.atg = 0;
}
else if textField == weightTextField2
{
weightTextField1.atg = 0;
weightTextField2.atg = 1;
}
picker.reloadAllComponents()
}
}

How to create multiple pickerviews programmatically

I am trying to create a separate pickerview for each textbox that is selected. so far the closest I have gotten is having the same pickerview for each of the text boxes that need their own separate list of items to choose from.
class MainViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
var dogBreedPickerArray = ["German Shepherd", "Huskie", "cray cray"]
var catBreedPickerArray = ["savanna", "cheetoh", "bengal"]
var maleFemalePickerArray = ["Male", "Female"]
var dogBreedPicker = UIPickerView()
var dogBreedPicker = UIPickerView()
var catBreedPicker = UIPickerView()
var maleFemalePicker = UIPickerView()
#IBOutlet var dogBreedTextBox: UITextField!
#IBOutlet var catBreedTextBox: UITextField!
#IBOutlet var maleFemaleTextBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dogBreedPicker.delegate = self
dogBreedPicker.dataSource = self
dogBreedTextBox.inputView = dogBreedPicker
dogBreedTextBox.delegate = self
}
// dog picker functions
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dogBreedPickerArray.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
petBreedTextBox.text = dogBreedPickerArray[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dogBreedPickerArray[row]
}
}
its just a example may be it help to you
after selecting any text field asign textfield tag to picker and reload the component.
// variable
var heightInch:[Int] = [Int]()
var heightfeet:[Int] = [Int]()
var heightCms:[Int] = [Int]()
delegate methods
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if picker.tag == 1{
return 2
}else{
return 1
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if picker.tag == 1{
switch component {
case 0:
return heightfeet.count
default:
return heightInch.count
}
}else{
return heightCms.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if picker.tag == 1{
switch component {
case 0:
return String(heightfeet[row])
default:
return String(heightInch[row])
}
}else{
return String(heightCms[row])
}

UIPickerView showing empty grey box Swift

I haven't a whole lot of code, so I might as well copy it here.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return false
}
}
The print statements are correct. For numberOfRows... and titleForRow it is printing the correct Strings.
But there is no prints for didSelectRow because, well, I can't select a row.
This is what I get:
You can ignore the Google Map in the background, that shouldn't interfere with the Picker View and is just set up in the StoryBoard.
The Grey window appears when I click on the textField but never shows any content. But the print statements say otherwise.
Does anyone know why this is the case?
Just add this line in your viewDidLoad method:
buildPickers.dataSource = self
And your code will be:
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.dataSource = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
And it will show your data.
UPDATE:
It is not showing because you set it hidden in your viewDidLoad.
Just remove this line from your code:
buildPickers.hidden = true
Here is your working code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true
buildText.delegate = self //set delegate for textField
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
//this method will call when you click on textField
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return true
}
}
Is the delegate set on the text field? Are you getting the callback which you use to un-hide the picker?

Resources