简体   繁体   English

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

[英]Update Label by using pickerview and text field

I have a PickerView that deposit values. 我有一个PickerView可以存入价值。 This values will be calculate with a user textfield input. 将使用用户文本字段输入来计算此值。 The result will show in a label. 结果将显示在标签中。 That works for me. 这对我行得通。 By starting the app the textfield have a fix number of 1. How can I now update my label automatically by use the Pickerview? 通过启动应用程序,文本字段的固定数字为1。现在如何使用Pickerview自动更新标签? I read and try many things but it don´t works. 我阅读并尝试了很多东西,但是没有用。 Maybe it is a little thing, but i don´t see it. 也许这是一件小事,但我看不到。

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)

    }

In the didSelectRow method for the pickerView , set the label's text to your newly calculated value. pickerViewdidSelectRow方法中,将标签的文本设置为新计算的值。

Update : To update the label's text depending on the textField's input, inside your ViewController 's viewDidLoad method, add this line 更新 :要根据textField的输入更新标签的文本,请在ViewControllerviewDidLoad方法内,添加以下行

AnzahlStab.delegate = self

and then add the textFieldDidEndEditing method, and inside it update the label's text to the new value. 然后添加textFieldDidEndEditing方法,并在其内部将标签的文本更新为新值。 This method is called when you are done with editing the text field. 编辑完文本字段后,将调用此方法。

Need to call function test in picker view didSelectRow . 需要在选择器视图didSelectRow调用功能测试。 Or Simply Replace your didSelectRow with following code. 或简单地将您的didSelectRow替换为以下代码。

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

Hope this Answer will help you. 希望这个答案能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM