我正在开发一个应用程序,用户输入一个文本字段值,然后乘以UIPickerView的didSelectRow方法中存储的值。这当前正在工作,但只有当我输入值并触摸/选择选择器时。一旦将值输入文本字段,如何对所选行执行计算?谢谢。
class Example2ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var inputField: UITextField!
@IBOutlet weak var resultsLabel: UILabel!
@IBOutlet weak var calcLabel: UILabel!
@IBOutlet weak var myPicker: UIPickerView!
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
myPicker.delegate = self
myPicker.dataSource = self
self.resultsLabel.text = ""
self.calcLabel.text = ""
pickerData = ["Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6"]
myPicker.selectRow(1, inComponent: 0, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print(string)
return true
}
@IBAction func editingChanged(sender: UITextField) {
sender.text = sender.text?.uppercaseString
inputField.text = sender.text
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
view.endEditing(true)
super.touchesBegan(touches, withEvent: event)
}
//UI Pickerview
// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var multiplicator : Double = 1.0
if (row == 0)
{
resultsLabel.text = "Item 1"
multiplicator = 1.00
}
else if (row == 1)
{
resultsLabel.text = "Item 2"
multiplicator = 2.00
}
else if (row == 2)
{
resultsLabel.text = "Item 3"
multiplicator = 3.00
}
else if (row == 3)
{
resultsLabel.text = "Item 4"
multiplicator = 4.00
}
else if (row == 4)
{
resultsLabel.text = "Item 5"
multiplicator = 5.00
}
else if (row == 5)
{
resultsLabel.text = "Item 6"
multiplicator = 6.00
}
let result = multiplicator * (inputField.text! as NSString).doubleValue
calcLabel.text = "Results: \(result)"
}
}
答案 0 :(得分:1)
我尝试了一些更简单的方法。编辑完成后,我对UItextField进行了@IBAction调用,然后在其中进行了所有后选择器选择。
@IBAction func textField_pickerAction(_ sender: Any) {
let row = picker.selectedRow(inComponent: 0)
...
...
...
}
答案 1 :(得分:0)
您可以通过将乘数转换为全局变量来实现此目的,然后您可以在textfield函数中使用它:
var multiplicator:Double!
@IBAction func editingChanged(sender: UITextField) {
sender.text = sender.text?.uppercaseString
inputField.text = sender.text
let result = multiplicator * (inputField.text! as NSString).doubleValue
calcLabel.text = "Results: \(result)"
}
您现在可以明显删除pickerview func中的最后两行代码
答案 2 :(得分:0)
除了全局var之外,我还通过将UIPickerView if语句添加到它自己的函数中来实现这一点。现在我可以随时随地拨打电话。
我正在使用以下命令从inputField实时更新calcLabel:
inputField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
func textFieldDidChange(textField: UITextField) {
pickerFunc()
}