Dropdown menu in xcode using picker view and textbox(swift)

By: Charles Xavier

85   19   25206

Uploaded on 07/29/2016

New Video with dropdown for swift 3.0: https://www.youtube.com/watch?v=71ZV4Hlxk1I

Updated.. Download this file: (bugs free)
https://www.dropbox.com/s/l8rflh1oygtcerh/single%20dropdown.txt?dl=0

Dropdown menu in swift using picker view and a text box. Code is here:

http://stackoverflow.com/questions/30319718/making-a-drop-down-list-using-swift/38666026#38666026

Comments (2):

By anonymous    2017-09-20

(Swift 3) Add text box and uipickerview to the storyboard then add delegate and data source to uipickerview and add delegate to textbox. Follow video for assistance https://youtu.be/SfjZwgxlwcc

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {


@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var dropDown: UIPickerView!


var list = ["1", "2", "3"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


public func numberOfComponents(in pickerView: UIPickerView) -> Int{
    return 1

}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{

    return list.count

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    self.view.endEditing(true)
    return list[row]

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    self.textBox.text = self.list[row]
    self.dropDown.isHidden = true

}

func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField == self.textBox {
        self.dropDown.isHidden = false
        //if you dont want the users to se the keyboard type:

        textField.endEditing(true)
    }

}
}

Original Thread

Recommended Books

    Popular Videos 730

    Submit Your Video

    If you have some great dev videos to share, please fill out this form.