Установить значение по умолчанию UIPickerView при касании UITextField - PullRequest
0 голосов
/ 23 мая 2018

Я хочу сохранить тот же текст заполнителя в моем UITextField, который называется «Местоположение».После касания UITextField я хочу, чтобы отображалось первое значение UIPickerView.Теперь пользователь должен прокрутить вниз, а затем обратно, чтобы значение отобразилось в UITextField.Я хочу, чтобы значение отображалось в UITextField, как только откроется UIPickerView. невыбрано, selected-UITextField по-прежнему не показывает значения

override func viewDidLoad() {
    let thePicker = UIPickerView()
    thePicker.delegate = self
    location.inputView = thePicker
    thePicker.selectRow(1, inComponent: 0, animated: true)
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickOptions.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickOptions[row]
}


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    location.text = pickOptions[row]
}

Ответы [ 2 ]

0 голосов
/ 23 мая 2018
import UIKit

class ActivityPersonDetail : UIViewController,UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate {


    let salutations = ["1", "2", "3","4","5","6","7","8","9","10","11","12"]

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return salutations.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return salutations[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerTF.text = salutations[row]
        picker.isHidden = true

    }

    func textFieldDidBeginEditing(_ textField: UITextField) {

        if textField == self.pickerTF {
            picker.isHidden = false
            textField.endEditing(true)
        }
    }


    lazy var picker: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        pickerView.backgroundColor = .white

        pickerTF.inputView = pickerView
        return pickerView
    }()

    let  arrowImage: UIImageView = {

        let image = UIImageView()
        image.translatesAutoresizingMaskIntoConstraints = false
        image.image = #imageLiteral(resourceName: "down_arrow")
        image.contentMode = .scaleAspectFit
        return image
    } ()

    lazy var pickerTF: UITextField = {

        let tf = UITextField()
        tf.delegate = self
        tf.translatesAutoresizingMaskIntoConstraints = false
        tf.textColor = .black
        tf.font = .systemFont(ofSize: 24, weight: .medium)
        tf.placeholder = salutations[0]
        return tf
    }()



    //
    // MARK :- viewDidLoad ============================================================================
    //
    private var pickerView: UIPickerView!

    override func viewDidLoad() {

        super .viewDidLoad()
        view.backgroundColor = .white

        setupNavigationBar()
        setupViews()
        setupAutoLayout()

    }

    // ================ setupNavigationBar =============

    func setupNavigationBar(){

        title = ""
        hideBackTitle()
        navigationController?.navigationBar.barTintColor = .navBar

    }
    func hideBackTitle() {
        let backbarItem = UIBarButtonItem()
        backbarItem.title = ""
        navigationItem.backBarButtonItem = backbarItem
    }


    // ================ setupViews =============

    func setupViews(){

        view.addSubview(pickerTF)
        pickerTF.addSubview(arrowImage)
        view.addSubview(picker)
        picker.isHidden = true
    }


    // ================ setupAutoLayout =============

    func setupAutoLayout() {

        pickerTF.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 80).isActive = true
        pickerTF.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
        pickerTF.heightAnchor.constraint(equalToConstant: 60).isActive = true
        pickerTF.widthAnchor.constraint(equalToConstant: 70).isActive = true

        arrowImage.rightAnchor.constraint(equalTo: pickerTF.rightAnchor, constant: -15).isActive = true
        arrowImage.topAnchor.constraint(equalTo: pickerTF.topAnchor, constant: 16).isActive = true
        arrowImage.widthAnchor.constraint(equalToConstant: 22).isActive = true
        arrowImage.heightAnchor.constraint(equalToConstant: 22).isActive = true

        picker.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        picker.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        picker.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        picker.heightAnchor.constraint(equalToConstant: 100).isActive = true


    }

}
0 голосов
/ 23 мая 2018

Реализуйте метод делегата текстового поля textFieldDidBeginEditing и задайте текст текстового поля, если оно еще не имеет значения.

func textFieldDidBeginEditing(_ textField: UITextField) {
    if textField.text!.isEmpty {
        // set the text as needed
    }
}
...