我正在创建一个秒表,所以我创建了一个按钮并添加了一个用于启动秒表的动作,但是一旦调用它,我就会收到此错误:
2020-05-13 19:46:02.543555+1000 Timer Pro[27206:2613795] -[Timer_Pro.Stopwatch2ViewController updateStopwatch]: unrecognized selector sent to instance 0x7fb4d7425390
(lldb)
我正在做的是在秒表中显示分钟,秒和小数,它从0开始。每0.01秒小数上升,到99时,小数秒加一并重置小数,当秒数重设时达到59会重置并增加一分钟。我有一个膝部表,当按下膝部按钮时,它会将时间放在膝部表上。在我的UI上,我有一个开始按钮,按下该按钮会变成一个停止按钮,而在停止/开始按钮旁边,我有一个膝部按钮,当按下时会变成一个重置按钮。我在名为stop.png,start.png,reset.png和lap.png的文件中有4个按钮。这是我的代码:
import UIKit
class Stopwatch2ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var timer = Timer()
var minutes: Int = 0
var seconds: Int = 0
var fractions: Int = 0
var stopWatchString: String = ""
var startStopWatch: Bool = true
var addLap: Bool = false
@IBOutlet weak var stopwatchLabel: UILabel!
@IBOutlet weak var lapsTableView: UITableView!
@IBOutlet weak var startstopButton: UIButton!
@IBOutlet weak var lapresetButton: UIButton!
@IBAction func startStop(_ sender: Any) {
if startStopWatch == true {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: Selector(("updateStopwatch")), userInfo: nil, repeats: true)
startStopWatch = false
startstopButton.setImage(UIImage(named: "stop.pnng"), for: UIControl.State.normal)
lapresetButton.setImage(UIImage(named: "lap.png"), for: UIControl.State.normal)
addLap = true
}else {
timer.invalidate()
startStopWatch = true
startstopButton.setImage(UIImage(named: "start.png"), for: .normal)
lapresetButton.setImage(UIImage(named: "reset.png"), for: .normal)
addLap = false
}
}
@IBAction func lapReset(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
stopwatchLabel.text = "00:00.00"
}
func updateStopWatch() {
fractions += 1
if fractions == 100 {
seconds += 1
fractions = 0
}
if seconds == 60 {
minutes += 1
seconds = 0
}
let fractionsString = fractions > 9 ? "\(fractions)" : "0\(fractions)"
let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"
stopWatchString = "\(minutesString):\(secondsString).\(fractionsString)"
stopwatchLabel.text = stopWatchString
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "Cell")
cell.backgroundColor = self.view.backgroundColor
cell.textLabel?.text = "Lap"
cell.detailTextLabel?.text = "00:00:00"
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
}
如果有人能够帮助我,将不胜感激,因为我是一个初学者,这个错误使我在深夜里熬了几个月。
您正在创建Selector(("updateStopwatch")
,但您的方法称为func updateStopWatch()
。
改为使用#selector(),这只能让您在实际存在的方法上创建选择器。