我正在创建一个应用程序,其中文本字段输入在标签上显示另一个文本字段上指示的时间量(以秒为单位)。我的应用程序仅在var seconds
设置为整数而不是文本字段输入时运行。当我将var seconds
设置为文本字段输入时,我在指定的代码行中收到以下错误:'ViewController' does not have a member named 'seconds'
任何人都可以提供帮助吗?感谢。
以下是我的所有代码,错误代码行旁边有//Error
:
class ViewController: UIViewController {
@IBOutlet weak var chronometre: UILabel!
@IBOutlet weak var prochainpointLabel: UILabel!
@IBOutlet weak var pointactuelLabel: UILabel!
@IBOutlet weak var point1Textfield: UITextField!
@IBOutlet weak var point1tempsTextfield: UITextField!
@IBOutlet weak var point2Textfield: UITextField!
@IBOutlet weak var point2tempsTextfield: UITextField!
var timer = NSTimer()
var seconds = point1tempsTextfield.text.toInt()
func subtractTime(dt:NSTimer){
seconds-- //Error
chronometre.text = "\(seconds)" //Error
if(seconds == 0) { //Error
seconds = point2tempsTextfield.text.toInt()
pointactuelLabel.text = point2Textfield.text
prochainpointLabel.text = nil
}
}
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 presenterButton(sender: AnyObject) {
pointactuelLabel.text = point1Textfield.text
prochainpointLabel.text = point2Textfield.text
chronometre.text = "\(seconds)" //Error
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: ("subtractTime:"), userInfo: nil, repeats: true)
}
}
感谢您的帮助,我对编码非常陌生,非常感谢!
答案 0 :(得分:0)
忘记使用计时器来计算时间。您应该在两个日期之间使用NSTimeInterval来测量经过的时间。
extension NSTimeInterval {
var time:String {
return String(format:"%02d:%02d", Int((self/60.0)%60), Int((self) % 60 ))
}
}
var startTime:NSTimeInterval = 0
var elapsedTime:NSTimeInterval = 0
startTime = NSDate().timeIntervalSinceReferenceDate // 446644854.4675
sleep(3) //
elapsedTime = NSDate().timeIntervalSinceReferenceDate - startTime // 3.005680024623871
println(elapsedTime.time) "00:03"
//将此概念应用于您的代码将如下所示:
class ViewController: UIViewController {
@IBOutlet weak var chronometre: UILabel!
@IBOutlet weak var prochainpointLabel: UILabel!
@IBOutlet weak var pointactuelLabel: UILabel!
@IBOutlet weak var point1Textfield: UITextField!
@IBOutlet weak var point1tempsTextfield: UITextField!
@IBOutlet weak var point2Textfield: UITextField!
@IBOutlet weak var point2tempsTextfield: UITextField!
var timer = NSTimer()
var startTime:NSTimeInterval = 0
func updateTime(){
chronometre.text = (NSDate().timeIntervalSinceReferenceDate - startTime).time
}
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 presenterButton(sender: AnyObject) {
pointactuelLabel.text = point1Textfield.text
prochainpointLabel.text = point2Textfield.text
timer = NSTimer.scheduledTimerWithTimeInterval(1/10, target: self, selector: ("updateTime:"), userInfo: nil, repeats: true)
startTime = NSDate().timeIntervalSinceReferenceDate
}
}