UITextField InputView需要一个完成按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITextField InputView需要一个完成按钮相关的知识,希望对你有一定的参考价值。

所以我觉得好像我在这上面花了很多时间。我有一个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.
    }


}
答案

像这样添加:

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

以上是关于UITextField InputView需要一个完成按钮的主要内容,如果未能解决你的问题,请参考以下文章

UITextField 自定义 InputView - 如何让 textFieldShouldReturn 触发?

iOS:自定义 UITextField 的 inputView 大小

UIPickerView 作为多个 UITextField 的 inputView

UITableView 作为 UITextField didSelectRowAtIndexPath 的 inputView 随机未触发

在 UITableView 重新加载时以编程方式打开/关闭 UITextField 的 inputView

UITableView 中的 UITextField 与 DatePicker 的 InputView