如何使用Swift在UIStepper的文本字段中输入整数?
这就是我在Objective C中的表现:
- (IBAction)oneClicker:(UIStepper *)sender {
self.oneValue.text = [NSString stringWithFormat:@"%d",
[[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue]];
AudioServicesPlaySystemSound(_PlaySound);
}
答案 0 :(得分:8)
这很简单
@IBOutlet weak var oneValue: UITextField!
@IBAction func oneClicker(sender: UIStepper) {
self.oneValue.text = Int(sender.value).description
}
答案 1 :(得分:3)
以下是我的工作方式我不知道最好的方法,但这里是
@IBOutlet weak var oneClicker: UIStepper!
@IBAction func oneClicker(sender: UIStepper) {
oneValue.text = "\(Int(oneClicker.value))"
}
答案 2 :(得分:1)
在尝试上面时,出现了错误。尝试了不同的东西,它起作用了。
@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))"
}