Экспорт нескольких выбранных значений в pickerView в числа на UILabel - PullRequest
0 голосов
/ 05 июня 2018

Я пытаюсь экспортировать результат моих «pickerViews» рядом друг с другом в числовой форме, как если бы «pickerViews» выбрал «A», «B» и «C», я хочуfunc ', чтобы распечатать их на' UILabel 'как «123» (каждое число для каждой буквы).Вот как я кодировал свой 'pickerView':

import UIKit

class PickerTextField: UITextField,UIPickerViewDelegate,UIPickerViewDataSource {

let pickerView = UIPickerView()
var itemList = [String]()

override init(frame: CGRect) {
    super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
@objc func textEdited(_ sender:PickerTextField)
{
    self.text = itemList[pickerView.selectedRow(inComponent: 0)]
}

override func draw(_ rect: CGRect) {
    super.draw(rect)
    self.tintColor = UIColor.clear
    self.addTarget(self, action: #selector(textEdited(_:)), for: .editingChanged)
    pickerView.showsSelectionIndicator = true
    pickerView.delegate = self
    pickerView.dataSource = self
    self.inputView = pickerView

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = .black
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(doneBtnAction(_:)))

    toolBar.items = [cancelButton, spaceButton, doneButton]
    self.inputAccessoryView = toolBar
}
@objc func doneBtnAction(_ sender:UIBarButtonItem) {
    resignFirstResponder()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return itemList.count
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let title = itemList[row]
    return NSAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor:UIColor.black])
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.text = itemList[row]
}
}

class ViewController: UIViewController {

    @IBOutlet weak var servicesField: PickerTextField!
    @IBOutlet weak var brandsField: PickerTextField!
    @IBOutlet weak var modelsField: PickerTextField!
    @IBOutlet weak var Capacity: UITextField!
    @IBOutlet weak var Details: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
        servicesField.itemList = ["Select", "Services", "others"]
        brandsField.itemList = ["Select" ,"Apple", "Samsung"]
        modelsField.itemList = ["Select", "5", "5s"]
    }

   }

любая помощь здесь?Спасибо.

1 Ответ

0 голосов
/ 05 июня 2018

Создайте структуру для ваших данных

struct Item {
  var id:Int
  var title:String
}

Измените класс PickerTextFiled, чтобы использовать эту структуру

class PickerTextField: UITextField,UIPickerViewDelegate,UIPickerViewDataSource {

    let pickerView = UIPickerView()
    var itemList = [Item]()
    var selectedItem:Item?

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    @objc func textEdited(_ sender:PickerTextField)
    {
        self.text = itemList[pickerView.selectedRow(inComponent: 0)].title
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.tintColor = UIColor.clear
        self.addTarget(self, action: #selector(textEdited(_:)), for: .editingChanged)
        pickerView.showsSelectionIndicator = true
        pickerView.delegate = self
        pickerView.dataSource = self
        self.inputView = pickerView

        let toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.default
        toolBar.isTranslucent = true
        toolBar.tintColor = .black
        toolBar.sizeToFit()

        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(doneBtnAction(_:)))

        toolBar.items = [cancelButton, spaceButton, doneButton]
        self.inputAccessoryView = toolBar
    }
    @objc func doneBtnAction(_ sender:UIBarButtonItem) {
        resignFirstResponder()
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return itemList.count
    }
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let title = itemList[row].title
        return NSAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor:UIColor.black])
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.selectedItem = itemList[row]
        self.text = itemList[row].title

    }
}

Затем вы можете получить идентификатор там, где вам нужно

class ViewController: UIViewController {

    @IBOutlet weak var servicesField: PickerTextField!
    @IBOutlet weak var brandsField: PickerTextField!
    @IBOutlet weak var modelsField: PickerTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        servicesField.itemList = [Item(id: -1, title: "Select"),Item(id: -1, title: "Services"),Item(id: -1, title: "others")]
        brandsField.itemList = [Item(id: -1, title: "Select"),Item(id: -1, title: "Apple"),Item(id: -1, title: "Samsung")]
        modelsField.itemList = [Item(id: -1, title: "Select"),Item(id: -1, title: "5"),Item(id: -1, title: "5s")]
    }
    func printAll() {

        if let servicesFieldId = servicesField.selectedItem?.id, let brandsFieldId = brandsField.selectedItem?.id, let modelsFieldId = servicesField.selectedItem?.id {
            label.text = String(servicesFieldId) + String(brandsFieldId) + String(modelsFieldId)
        }
    }
}
...