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

General Tech Bugs & Fixes 2 years ago

1 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 31 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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 = 
                                                
                                                
1 views
0 shares
profilepic.png
manpreet 2 years 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 
                                                    
                                                    
1 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.