我正在尝试实现一个简单的步进器,它将其值反射(reflect)到标签上。 使用“Int(sender.value)”时,我收到错误“值的使用不明确”
// ViewController.swift
// Stepper
//
// Created by Prabhu Konchada on 19/06/15.
// Copyright (c) 2015 Prabhu. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var StepperValue: UILabel!
@IBOutlet weak var OutputLabel: UILabel!
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 StepperTap(sender: AnyObject) {
self.OutputLabel.text = String(Int(sender.value).description)
}
}
最佳答案
您可以执行以下操作:
@IBOutlet var lblStep: UILabel!
@IBAction func stepPressed(sender: UIStepper) {
lblStep.text = sender.value.description
}
否则你必须将AnyObject
转换为UIStepper
就像一样
var stepControl : UIStepper = sender as! UIStepper
就像:
@IBAction func StepperTap(sender: AnyObject) {
var stepControl : UIStepper = sender as! UIStepper
self.OutputLabel.text = stepControl.value.description
}
整个代码为 6.1 版本,您只需更改为 as 即可!对于 6.3.2:
import UIKit
class ViewController: UIViewController {
@IBOutlet var lblStep: UILabel!
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 stepPressed(sender: AnyObject) {
var step : UIStepper = sender as UIStepper
lblStep.text = step.value.description
}
}
关于iphone - 实现 UIStepper 时对 "value"的使用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950447/