如何使用UISepper将整数放入Swift的文本字段中

nxagd54h  于 2023-09-30  发布在  Swift
关注(0)|答案(4)|浏览(98)

我如何使用Swift从UISepper中将整数放入文本字段?
我在Objective C中是这样做的:

- (IBAction)oneClicker:(UIStepper *)sender {
    self.oneValue.text = [NSString stringWithFormat:@"%d",
                          [[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue]];
    AudioServicesPlaySystemSound(_PlaySound);
}
3pmvbmvn

3pmvbmvn1#

很简单

@IBOutlet weak var oneValue: UITextField!

@IBAction func oneClicker(sender: UIStepper) {
    self.oneValue.text = Int(sender.value).description
}
kcrjzv8t

kcrjzv8t2#

这是我如何让它工作不知道是否最好的方法,但它是在这里

@IBOutlet weak var oneClicker: UIStepper!

 @IBAction func oneClicker(sender: UIStepper) {
    oneValue.text =  "\(Int(oneClicker.value))"
        }
aurhwmvo

aurhwmvo3#

在上面尝试时,得到了一个错误。尝试了一些不同的东西,它的工作。

@IBOutlet var quantity: UILabel!    
@IBOutlet var StepCounter: UIStepper!
override func viewDidLoad() {
    super.viewDidLoad()

    StepCounter.autorepeat = true
    StepCounter.maximumValue = 10.0
    StepCounter.minimumValue = 1.0
    println(StepCounter.value)
    quantity.text = "\(Int(StepCounter.value))"
    StepCounter.addTarget(self, action: "stepperValueDidChange:", forControlEvents: .ValueChanged)

}

func stepperValueDidChange(stepper: UIStepper) {

    let stepperMapping: [UIStepper: UILabel] = [StepCounter: quantity]

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

w80xi6nr4#

@IBOutlet weak var splitNumLabel: UILabel!

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

相关问题