在“UIPickerView”功能设置上接收错误
Posted
技术标签:
【中文标题】在“UIPickerView”功能设置上接收错误【英文标题】:Receiving errors on 'UIPickerView' func settings 【发布时间】:2018-06-04 16:57:09 【问题描述】:我在一个应用程序中有两个(或更多)文本字段,我希望每个文本字段在被选中时弹出自己的 Pickerview 轮...这是我目前所得到的:
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
但它一直给我错误提示 "Missing return in a function expected to return String?"
和 "Missing return in a function expected to return int"
在我评论的地方。我能做什么?
【问题讨论】:
【参考方案1】:如果你想在pickerview中使用多个文本字段,你可以使用这个 UITextField 子类。
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]
并在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"]
将子类分配给文本字段。
结果
【讨论】:
伟大的编码,谢谢。【参考方案2】:您需要退回else
案例。
if serviceType.isFirstResponder
return serviceArray.count
else if brandsField.isFirstResponder
return brandsArray.count
else
// insert your else case here
return 0 // for example
【讨论】:
【参考方案3】:将这 3 个方法重构为 this ,因为如果所有 if 情况都没有命中,编译器将不知道返回什么,所以它给出了这个错误
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
【讨论】:
以上是关于在“UIPickerView”功能设置上接收错误的主要内容,如果未能解决你的问题,请参考以下文章
如何为 UIPickerView 行设置 Voice Over 辅助功能标签?