gpt4 book ai didi

ios - 从 UITextField 更改后如何更改 UIStepper 值?

转载 作者:行者123 更新时间:2023-11-30 13:36:48 25 4
gpt4 key购买 nike

我对这一切都很陌生,我发现了一些代码让我理解了一些语法。我正在尝试创建一个文本字段,让我输入一个值来更新步进器的值。步进器当前可以工作(更新 uitextfield),但是当我更改文本字段中的值时,它不会更新步进器的值,因此当我单击步进器时,它会恢复到我输入值之前的任何值。 ..谁能告诉我为什么 STracksValueDidChange 和 CTrackValueDidChange 这两个函数有错误?

这是我到目前为止的代码:

import UIKit

ViewController 类:UIViewController {

@IBOutlet weak var STracks: UITextField!
@IBOutlet weak var STracksStepper: UIStepper!
@IBOutlet weak var CTracks: UITextField!
@IBOutlet weak var CTrackStepper: UIStepper!



override func viewDidLoad() {
super.viewDidLoad()


STracksStepper.autorepeat = true
STracksStepper.maximumValue = 100.0
STracksStepper.minimumValue = 2.0
STracksStepper.stepValue = 2.0
print(STracksStepper.value)
STracks.text = "\(Int(STracksStepper.value))"
STracksStepper.addTarget(self, action: "SstepperValueDidChange:", forControlEvents: .ValueChanged)
STracks.addTarget(self, action: "STextValueDidChange:", forControlEvents: .ValueChanged)


CTrackStepper.autorepeat = true
CTrackStepper.maximumValue = 100.0
CTrackStepper.minimumValue = 2.0
CTrackStepper.stepValue = 2.0
print(CTrackStepper.value)
CTracks.text = "\(Int(CTrackStepper.value))"
CTrackStepper.addTarget(self, action: "CstepperValueDidChange:", forControlEvents: .ValueChanged)
CTracks.addTarget(self, action: "CTextValueDidChange:", forControlEvents: .ValueChanged)

}

//Steppers will update UITextFields
func SstepperValueDidChange(stepper: UIStepper) {

let stepperMapping: [UIStepper: UITextField] = [STracksStepper: STracks]

stepperMapping[stepper]!.text = "\(Int(stepper.value))"
}

func STracksValueDidChange(SText: UITextField) {

let STextMapping: [UITextField: UIStepper] = [STracks: STracksStepper]

STextMapping[SText]!.value = "(SText.text)"
}


func CstepperValueDidChange(stepper: UIStepper) {

let stepperMapping: [UIStepper: UITextField] = [CTrackStepper: CTracks]

stepperMapping[stepper]!.text = "\(Int(stepper.value))"
}


func CTrackValueDidChange(CText: UITextField) {

let CTextMapping: [UITextField: UIStepper] = [CTracks: CTrackStepper]

CTextMapping[CText]!.value = "(CText.text)"
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

最佳答案

尝试这样的事情。

CTrackStepper.value = Double(Textfield.text)

我不太确定您的代码中的映射是什么。
但我认为你不需要它来改变值。

更新,自己做了一个项目:

 import UIKit

class ViewController: UIViewController {

@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var stepper: UIStepper!


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

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func stepperValueChanged(sender: UIStepper) {
textfield.text = String(sender.value)
}

@IBAction func valueChanged(sender: UITextField) {
if Double(sender.text!) != nil {
stepper.value = Double(sender.text!)!
}
}
}

对于steppervaluechanged和valuechanged,只需从uistepper和文本字段拖动并选择操作并将Anyobject更改为Uitextfield的Uistepper。

祝你好运:)

关于ios - 从 UITextField 更改后如何更改 UIStepper 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com