UITextField InputView 需要一个完成按钮
Posted
技术标签:
【中文标题】UITextField InputView 需要一个完成按钮【英文标题】:UITextField InputView needs a done button 【发布时间】:2016-03-25 03:33:40 【问题描述】:所以我觉得我在这方面花了很多时间。我有一个UIPickerView
,它是通过带有UITextField
的InputView 以编程方式制作的。
我的问题是,我需要这些 UIPickerViews
在辞职第一响应者时有一个完成按钮!
如您所见,我尝试在工具栏上将 var climateToolBar 放在顶部,然后在 viewDidLoad
部分调用climateToolBar。
感谢您的帮助。
这是我的代码:
import UIKit
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate
@IBOutlet weak var climateText: UITextField!
@IBOutlet weak var environmentText: UITextField!
@IBOutlet weak var regionText: UITextField!
@IBOutlet weak var budgetText: UITextField!
var climatePicker = UIPickerView()
var environmentPicker = UIPickerView()
var regionPicker = UIPickerView()
var budgetPicker = UIPickerView()
var climateToolBar = UIToolbar()
//MARK: Arrays for PickerViews
var climates = ["Hot","Warm","Cold","Anywhere"]
var environment = ["Beach","City","Mountain","Rural", "Anywhere"]
var region = ["Central America","Carribean","Europe","North America","South America", "Anywhere"]
var budget = ["$0-$500","$501-$1000","$1001-$1500","$1501-$2500","$2501 and up"]
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
climatePicker.tag = 0
environmentPicker.tag = 1
regionPicker.tag = 2
budgetPicker.tag = 3
climateText.inputView = climatePicker
climateText.inputAccessoryView = climateToolBar
environmentText.inputView = environmentPicker
regionText.inputView = regionPicker
budgetText.inputView = budgetPicker
climatePicker.delegate = self
environmentPicker.delegate = self
regionPicker.delegate = self
budgetPicker.delegate = self
climatePicker.dataSource = self
environmentPicker.dataSource = self
regionPicker.dataSource = self
budgetPicker.dataSource = self
climateToolBar = UIToolbar()
climateToolBar.barStyle = UIBarStyle.Default
climateToolBar.translucent = true
climateToolBar.sizeToFit()
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return 1
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if climatePicker.tag == 0
return climates.count
else if environmentPicker.tag == 1
return environment.count
else if regionPicker.tag == 2
return region.count
else if budgetPicker.tag == 3
return budget.count
return 1
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if pickerView.tag == 0
return climates[row]
else if pickerView.tag == 1
return environment[row]
else if pickerView.tag == 2
return region[row]
else if pickerView.tag == 3
return budget[row]
return ""
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if pickerView.tag == 0
climateText.text = climates[row]
else if pickerView.tag == 1
environmentText.text = environment[row]
else if pickerView.tag == 2
regionText.text = region[row]
else if pickerView.tag == 3
budgetText.text = budget[row]
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
【参考方案1】:像这样添加:
let pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
pickerView.backgroundColor = .whiteColor()
pickerView.showsSelectionIndicator = true
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "canclePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
textField.inputView = pickerView
textField.inputAccessoryView = toolBar
【讨论】:
我不久前在另一个字符串中看到了这段代码,我不确定如何将 let 值实现为 swift。 另外,我是否声称视图上方的工具栏将部分加载为 var? 是的,你可以,但因为它是一个常数,它应该是'让'。 明白了。好吧,这个问题对我来说很轻松,因为我对这一切还很陌生,并试图重新学习编码,但我是否将所有这些都放在视图下加载? 是的,可以放在viewDidLoad中以上是关于UITextField InputView 需要一个完成按钮的主要内容,如果未能解决你的问题,请参考以下文章
iOS:自定义 UITextField 的 inputView 大小
UITextField 自定义 InputView - 如何让 textFieldShouldReturn 触发?
UITableView 中的 UITextField 与 DatePicker 的 InputView
UIPickerView 作为多个 UITextField 的 inputView
UITableView 作为 UITextField didSelectRowAtIndexPath 的 inputView 随机未触发