将多个pickerView选定值导出到UILabel上的数字

时间:2018-06-05 12:08:35

标签: swift uilabel uipickerview func

我试图导出我的' pickerViews'它们以数字形式彼此相邻,如果' pickerViews'选择了" A"," B"和" C",我想要一个' func'将它们打印在一个' UILabel' as" 123" (每个字母的每个数字)。这就是我编写“我的选择器”的方式。 :

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 :(得分:1)

为您的数据创建结构

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

    }
}

然后您可以在任何需要的地方获取ID

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)
        }
    }
}