试图分别为几个文本字段制作选择器视图输入

时间:2016-07-12 02:33:21

标签: ios swift xcode

我正在使用swift制作GPA计算器,但我遇到了一些问题。我创建了一个包含所有字母等级(A到F)的选择器视图。我已经放了7个文本字段(每个课程一个)。我希望用户点按文本字段并显示选择器视图,所选的成绩将显示在文本字段中。我已经设法为第一个文本字段做了这个,但我不知道如何编写允许我为每个文本字段选择等级的代码。我尝试了不同的方法,但我总是最终改变第一个文本字段。

感谢任何帮助: - )

这是我的代码(gradeOne是课程1的文本字段,课程2的gradeTwo等):

导入UIKit

类GPA:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

@IBOutlet var gradeOne: UITextField!

@IBOutlet var gradeTwo: UITextField!





//Grades array

var grades = ["A+/A (85-100)", "A- (80-84)", "B+ (77-79)", "B (73-76)", "B- (70-72)", "C+ (67-69)", "C (63-66)", "C- (60-62)", "D+ (57-59)", "D (53-56)", "D- (50-52)", "F (0-49)"]


//Grade 1 Picker
func numberOfComponentsInPickerView(gradePickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(gradePickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return grades.count
}

func pickerView(gradePickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return grades[row]
}

func pickerView(gradePickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    gradeOne.text = grades[row]

}



//Make keyboard disappear


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let gradePickerView = UIPickerView()
    gradePickerView.delegate = self
    gradeOne.inputView = gradePickerView


    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    view.addGestureRecognizer(tap)

}

func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

}

3 个答案:

答案 0 :(得分:4)

您应该将gradePickerView设置为所有文本字段的inputView

gradeTwo.inputView = gradePickerView
gradeThree.inputView = gradePickerView
// set for rest of textfields

然后在您的pickerView:didSelectRow:方法中,您需要将文本设置为当前的活动文本字段。我建议使用UITextFieldDelegate

设置活动文本字段
var activeField: UITextField?

override func viewDidLoad() {
    // Set VC as textfield's delegate
    gradeTwo.delegate = self
    gradeThree.delegate = self
    // Set for rest of textfields
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    activeField = textField
}

func pickerView(gradePickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    activeField.text = grades[row]
}

答案 1 :(得分:0)

使用UITextField的委托将所有输入视图设置为pickerView。确保为所有文本字段设置委托

let gradePickerView = UIPickerView()
override func viewDidLoad() {
    super.viewDidLoad()

    // set delegates for all textfields

    gradePickerView.delegate = self
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    view.addGestureRecognizer(tap)

}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    textField.inputView = gradePickerView
}

答案 2 :(得分:0)

我想你想点击7个textfiel。每次pickerView都会显示。你在pickerView中选择一个单元格,而textfiel(你只是点击)会显示你的选择?如果这是真的。你应该做这个步骤: 1:为每个textFiel添加标签:yourtextFiel.view.tag = 0(或1,2,3,4,...) 2:当show pickerView:添加pickerView.tag = yourtextFiel.view.tag 3:在fucion pickerView didSelectRow:获取你的textFiel的标签将显示你选择的:let mytag = pickerView.tag。 使用“mytag”找到您的textFiel并更改您的文字 Googluck