Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

UIPickerView for each text field with different arrays (Swift/Firebase)

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Sarah Jones

User

( 7 months ago )

 

I am trying to create a form in which each text field has a UIPickerView which the user can use to select the option needed. I need a to use a different array of information for each text field but I can't seem to get it to work and I have been stuck on it for quite a while now.

I have had a method which was possibly working but I couldn't then retrieve the data from the text field for Firebase due to each field having the same name.

Here is my code so far, this is the latest method that I have tried that doesn't work:

import UIKit
import Firebase
import FirebaseDatabase

class CreatePostViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

@IBOutlet weak var gameTextField: UITextField!
@IBOutlet weak var activityTextField: UITextField!
@IBOutlet weak var consoleTextField: UITextField!
@IBOutlet weak var skillTextField: UITextField!
@IBOutlet weak var communicationTextField: UITextField!
@IBOutlet weak var lfglfmTextField: UITextField!
@IBOutlet weak var rulesTextView: UITextView!
@IBOutlet weak var descriptionTextView: UITextView!

var games = ["Assassin's Creed Origins", "Battlefield 1", "Call of Duty: Advanced Warfare", "Call of Duty: Black Ops III", "Call of Duty: Ghosts", "Call of Duty: Infinite Warfare", "Call of Duty: Modern Warfare Remastered", "Call of Duty: WWII", "Destiny", "Destiny 2", "Fifa 16", "Fifa 17", "Fifa 18", "Rocket League"]
var console = ["Xbox One", "Xbox 360", "Playstation 4", "Playstation 3"]
var skill = ["Achiever", "Explorer", "Killer", "Socializer"]
var communication = ["Mic", "No Mic"]
var lfglfm = ["LFG", "LFM"]

var itemSelected = ""

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //remove margin / padding from textview
    self.rulesTextView.textContainerInset = .zero
    self.rulesTextView.contentInset = UIEdgeInsetsMake(0, -5, 0, 0)
//        self.descriptionTextView.textContainerInset = .zero
//        self.descriptionTextView.contentInset = UIEdgeInsetsMake(0, -5, 0, 0)

    //allow tap on screen to remove text field input from screen
    self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

    //UIPICKER
    let pickerView = UIPickerView()
    pickerView.delegate = 
								 								 
							usr_profile.png
							
Yasmin Mirza

User

( 7 months ago )

 

Your code is almost right. I would just store a class attribute with the UIPickerView so you can reload the components upon textField editing status change. For that purpose, you have also to set the textField delegates.

Here a working example with the adjustments I just commented:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {


    @IBOutlet weak var gameTextField: UITextField!
    @IBOutlet weak var activityTextField: UITextField!
    @IBOutlet weak var consoleTextField: UITextField!
    @IBOutlet weak var skillTextField: UITextField!
    @IBOutlet weak var communicationTextField: UITextField!
    @IBOutlet weak var lfglfmTextField: UITextField!

    var games = ["Assassin's Creed Origins", "Battlefield 1", "Call of Duty: Advanced Warfare", "Call of Duty: Black Ops III", "Call of Duty: Ghosts", "Call of Duty: Infinite Warfare", "Call of Duty: Modern Warfare Remastered", "Call of Duty: WWII", "Destiny", "Destiny 2", "Fifa 16", "Fifa 17", "Fifa 18", "Rocket League"]
    var console = ["Xbox One", "Xbox 360", "Playstation 4", "Playstation 3"]
    var skill = ["Achiever", "Explorer", "Killer", "Socializer"]
    var communication = ["Mic", "No Mic"]
    var lfglfm = ["LFG", "LFM"]

    var itemSelected = ""

    weak var pickerView: UIPickerView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        //remove margin / padding from textview
        //        self.descriptionTextView.textContainerInset = .zero
        //        self.descriptionTextView.contentInset = UIEdgeInsetsMake(0, -5, 0, 0)

        //allow tap on screen to remove text field input from screen
        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

        //UIPICKER
        let pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self

        gameTextField.delegate = self
        consoleTextField.delegate = self
        skillTextField.delegate = self
        communicationTextField.delegate = self
        lfglfmTextField.delegate = self

        gameTextField.inputView = pickerView
        consoleTextField.inputView 
			
        

what's your interest