gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:49:46 26 4
gpt4 key购买 nike

我有一个存放值的 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)

}

最佳答案

pickerViewdidSelectRow 方法中,将标签的文本设置为新计算的值。

更新:要根据 textField 的输入更新标签的文本,请在 ViewControllerviewDidLoad 方法中添加此行

AnzahlStab.delegate = self

然后添加 textFieldDidEndEditing 方法,并在其中将标签的文本更新为新值。当您完成文本字段的编辑时,将调用此方法。

关于ios - 使用pickerview和文本字段更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434021/

26 4 0