2017-09-24 82 views
1

定时器作用域问题我一直坚持把它放到正确的范围内。我确定它的东西超级简单,但我用头撞墙。我发现任何答案是在早期版本的迅速,所以即时通讯努力了解如何解决这个#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. 
    } 
} 

即时通讯尝试创建一个可以使用手势进行控制的秒表。短按标签上的停止/开始,长按重置时间。

回答

1

在你的updateTimer()方法中,第一行应该改为seconds -= 1(如果你想倒计数)。

此外,您可能需要更新您的updateTimer()方法是这样的:

func updateTimer() { 

     seconds -= 1 

     if seconds == 0 { 
     timer.invalidate() 
     isTimerRunning = false 
     } 

     shortTimerLabel.text = String(describing: seconds) 
    } 

这里的另一个问题是,你加入你的runTimer()updateTimer()方法来错了地方。您不应该将加入您的viewDidLoad方法中。

你的最终代码应该是这样的:

var seconds = 60 
var timer = Timer() 
var isTimerRunning = false 

@IBAction func longpressed(_ gestureRecognizer: UILongPressGestureRecognizer) { 
    resetTimer() 
} 

@IBAction func tappedShortTimer(_ gestureRecognizer: UITapGestureRecognizer) { 
    stopStartTimer() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // ... 
} 

func stopStartTimer() { 

    if !isTimerRunning { 

     timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true) 

     isTimerRunning = true 

    } else { 

     timer.invalidate() 

     isTimerRunning = false 

    } 
} 

func updateTimer() { 

    seconds -= 1 

    if seconds == 0 { 
     timer.invalidate() 
     isTimerRunning = false 
    } 

    shortTimerLabel.text = String(describing: seconds) 
} 

func resetTimer() { 

    if isTimerRunning { 

     seconds = 60 

     timer.invalidate() 

     isTimerRunning = false 

     stopStartTimer() 
    } 
} 
+0

@ M4YDIE,如果此答案对您有帮助,请将其标记为已接受,以便可以帮助其他人查找相同的答案。 :)如果您有任何其他问题,请随时询问我! – Alex

+0

我仍然收到一个类似的错误,“#selector的参数是指实例方法'updateTimer()'没有公开给Objective-C”我应该注意到,即时通讯工作在“SecondViewController”,如果这有什么区别? – M4YDIE

+0

更改为选择器((“updateTimer”))似乎修复了错误,但它在按下按钮时崩溃 – M4YDIE

1
  • 的选择应在形式给出#selector(ViewController.updateTimer)
  • viewDidLoad你不应该申报功能,但是外界
  • 你只设置longpressed函数中的计时器
  • 对于停止它是timer.invalidate()
+1

由于他使用@IBActions来设置他的手势识别器(如果一切都正确连接),所以完全没有在viewDidLoad中声明#selector(ViewController.updateTimer)。 – Alex