从UITextField更改后,如何更改UIStepper值?

时间:2016-03-13 22:19:13

标签: ios swift uitextfield uistepper

我对这一切都很陌生,我找到了一些让我理解这种语法的代码。我正在尝试创建一个文本字段,让我输入一个更新步进器值的值。步进器当前工作(更新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.
}

}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

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和textfield拖动并选择action并将Anyobject更改为Uitextfield的Uistepper。

祝你好运:)