Mam dwa (lub więcej) pola tekstowe w aplikacji, a ja chcę każdy textfield, aby wyskoczyć własne koło Pickererview, gdy zostanie wybrane ... Oto, co mam do tej pory:

var pickerView = UIPickerView()

@IBOutlet weak var serviceType: UITextField!
@IBOutlet weak var brandsField: UITextField!

var serviceArray = ["Services", "..."]
var brandsArray = ["Apple", "Samsung"]

override func viewDidLoad() {
    super.viewDidLoad()

    pickerView.delegate = self
    pickerView.dataSource = self
}

func updatePicker(){
    self.pickerView.reloadAllComponents()
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if serviceType.isFirstResponder{
        return serviceArray.count
    } else if brandsField.isFirstResponder{
        return brandsArray.count
    }
} //ERROR HERE

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if serviceType.isFirstResponder{
        return serviceArray[row]
    }else if brandsField.isFirstResponder{
        return brandsArray[row]
    }
}  //ERROR HERE

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if serviceType.isFirstResponder{
        let itemselected = serviceArray[row]
        serviceType.text = itemselected
    }else if brandsField.isFirstResponder{
        let itemselected = brandsArray[row]
        brandsField.text = itemselected
    }
}

}

Ale prowadzi mi błąd mówiąc "Missing return in a function expected to return String?" i "Missing return in a function expected to return int" w miejscu, w którym skomentowałem. Co mogę zrobić?

0
Shahin 4 czerwiec 2018, 19:57

3 odpowiedzi

Najlepsza odpowiedź

Możesz użyć tego podklasy Uitextfield, jeśli chcesz użyć wielu tekstów z Pickerviewem.

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

I przypisz listę opcji w viewDidLoad}

class ViewController: UIViewController {

    @IBOutlet weak var serviceType: PickerTextField!
    @IBOutlet weak var brandsField: PickerTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        serviceType.itemList = ["Services", "others"]
        brandsField.itemList = ["Apple", "Samsung"]
    }
}

Przypisz podklasę do TextField.

enter image description here

Wynik

enter image description here

1
RajeshKumar R 4 czerwiec 2018, 17:48

Musisz zwrócić etui else.

if serviceType.isFirstResponder{
    return serviceArray.count
} else if brandsField.isFirstResponder{
    return brandsArray.count    
} else {
  // insert your else case here
  return 0 // for example
}
0
user9749232user9749232 4 czerwiec 2018, 17:00

Refaktor Te 3 metody, ponieważ kompilator nie będzie wiedział, co powrócić, jeśli wszystkie przypadki nie są trafione, więc daje ten błąd

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if serviceType.isFirstResponder{
        return serviceArray.count
    } else {
        return brandsArray.count
    }
} //ERROR HERE

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if serviceType.isFirstResponder{
        return serviceArray[row]
    }else {
        return brandsArray[row]
    }
}  //ERROR HERE

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if serviceType.isFirstResponder{
        let itemselected = serviceArray[row]
        serviceType.text = itemselected
    }else {
        let itemselected = brandsArray[row]
        brandsField.text = itemselected
    }
}
0
Sh_Khan 4 czerwiec 2018, 17:06