我正在Xcode中使用Swift 5编写应用程序代码。在一个视图控制器上,我正在创建一个计时器,该计时器从20分钟到0倒计时。我有一个成功的代码,但它会抛出一个错误。在行中
timer = Timer.scheduledTimer(timeInterval:0.01,target:self,选择器:#selector(SkippingViewController.updateTimer),userInfo:nil,重复:true)
它给出一个错误,指出类型“ SkippingViewController”没有成员“ updateTimer” (SkippingViewController是计时器所在的应用页面的视图控制器名称) 我该如何解决这个问题?
import UIKit
class SkippingViewController: UIViewController {
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var startWorkoutButton: UIButton!
@IBOutlet weak var pauseWorkoutButton: UIButton!
var timer = Timer()
var counter = 20.00
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "\(counter)"
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
// Do any additional setup after loading the view.
}
@IBAction func startWorkoutButtonDidTouch(_ sender: Any) {
if !isRunning {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
startWorkoutButton.isEnabled = false
pauseWorkoutButton.isEnabled = true
isRunning = true
}
}
@IBAction func pauseWorkoutButtonDidTouch(_ sender: Any) {
func updateTimer() {
counter -= 0.01
timeLabel.text = String(format: "%.01f", counter)
}
}
}
答案 0 :(得分:2)
您的问题是,SkippingViewController.swift中没有名为“ updateTimer”的方法。您错误地将方法放入方法'pauseWorkoutButtonDidTouch'内。为了解决该错误,请在SkippingViewController.swift中插入以下代码:
@objc func updateTimer() {
counter -= 0.01
timeLabel.text = String(format: "%.01f", counter)
}