定时器作用域问题我一直坚持把它放到正确的范围内。我确定它的东西超级简单,但我用头撞墙。我发现任何答案是在早期版本的迅速,所以即时通讯努力了解如何解决这个#selector
我目前的问题是试图让计时器正确初始化和计数。 “选择者”造成的问题最多。其余的我肯定是病得不行了
代码如下。
@IBOutlet weak var shortTimerLabel: UILabel!
@IBOutlet weak var longTimerLabel: UILabel!
var seconds = 60 //This variable will hold a starting value of seconds. It could be any amount above 0.
var timer = Timer()
var isTimerRunning = false //This will be used to make sure only one timer is created at a time.
@IBAction func longpressed(_ gestureRecognizer: UILongPressGestureRecognizer) {
shortTimerLabel.text = "longPressed"
}
@IBAction func tappedShortTimer(_ gestureRecognizer: UITapGestureRecognizer) {
shortTimerLabel.text = "ShortPressed"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer() {
seconds += 1 //This will decrement(count down)the seconds.
shortTimerLabel.text = "\(seconds)" //This will update the label.
}
}
即时通讯尝试创建一个可以使用手势进行控制的秒表。短按标签上的停止/开始,长按重置时间。
@ M4YDIE,如果此答案对您有帮助,请将其标记为已接受,以便可以帮助其他人查找相同的答案。 :)如果您有任何其他问题,请随时询问我! – Alex
我仍然收到一个类似的错误,“#selector的参数是指实例方法'updateTimer()'没有公开给Objective-C”我应该注意到,即时通讯工作在“SecondViewController”,如果这有什么区别? – M4YDIE
更改为选择器((“updateTimer”))似乎修复了错误,但它在按下按钮时崩溃 – M4YDIE