使用pickerview和文本字段更新标签

时间:2015-12-23 10:52:52

标签: ios swift ios7 swift2 xcode7

我有一个存放价值的PickerView。将使用用户文本字段输入计算此值。结果将显示在标签中。这对我行得通。 通过启动应用程序,文本字段的修订号为1。 我现在如何使用Pickerview自动更新我的标签? 我阅读并尝试了很多东西,但它不起作用。 也许这是一件小事,但我不明白。

import UIKit

class BalkenbewehrungVC: UIViewController, UIPopoverPresentationControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

    // Deklarationen Pickerview
    var multiplicator : Double = 0.0

    let multiplicators = [0.0,6.0,8.0,10.0,12.0,14.0,16.0,20.0,25.0,26.0,28.0,30.0,32.0,36.0,40.0,50.0]

    let PI = 3.1415

    // Pickerview füllen
    var pickerDataSource = ["---","Ø 6","Ø 8","Ø 10","Ø 12","Ø 14","Ø 16","Ø 20","Ø 25","Ø 26","Ø 28","Ø 30","Ø 32","Ø 36","Ø 40","Ø 50"]


    // Picker im aktivieren
    @IBOutlet weak var Pickerview: UIPickerView!


    // Ausgabewert As = xxxx cm
    @IBOutlet var AusgabePicker: UILabel!

    // Eingabe Anzahl Stäbe
    @IBOutlet var AnzahlStab: UITextField!

      @IBAction func test(sender: UITextField) {

        if AnzahlStab.text!.isEmpty {

            AnzahlStab.text = "1"

            let result1 = (multiplicator / 10) * (multiplicator / 10) * PI / 4 * 1

            AusgabePicker.text! = String(format:"%.3f", result1)


        } else {

            let result = (multiplicator / 10) * (multiplicator / 10) * PI / 4 * (AnzahlStab.text! as NSString).doubleValue
            // let resultString = "\(result)"


            AusgabePicker.text = String(format:"%.3f", result)
        }

    }

// Picker - feste Reihen
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    // Picker - Aufaddierung neuer Reihen
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerDataSource.count

    }

    // Picker - Datenübernahme multiplicator in Picker
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerDataSource[row]


    }

    // Picker - Wertefestlegung Picker
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        multiplicator = multiplicators[row]
        _ = NSNumberFormatter.localizedStringFromNumber(NSNumber(double: multiplicator), numberStyle:.DecimalStyle)

    }

2 个答案:

答案 0 :(得分:0)

didSelectRow的{​​{1}}方法中,将标签的文字设置为新计算的值。

更新:要根据text pickerView ViewController方法中的textField输入更新标签文字,添加此行

viewDidLoad

然后添加AnzahlStab.delegate = self 方法,并在其中将标签的文本更新为新值。完成编辑文本字段后,将调用此方法。

答案 1 :(得分:0)

需要在选择器视图didSelectRow中调用函数测试。或者只需使用以下代码替换didSelectRow即可。

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        multiplicator = multiplicators[row]
        _ = NSNumberFormatter.localizedStringFromNumber(NSNumber(double: multiplicator), numberStyle:.DecimalStyle)
        self.test(AnzahlStab)
    }

希望本答案能为您提供帮助。