寻找一些帮助,我有一个计时器,提交密码后工作很好,但我需要在计时器启动后禁用按钮一段时间,(在代码我已进入名义上的90秒)
然而按钮没有禁用。
如果有人能告诉我哪里出错了会很棒。
import UIKit
class appiHour:UIViewController {
var timer = Timer()
var counter = 60
var password_Text: UITextField?
func enableButton() {
self.timerStartButton.isEnabled = true
}
@IBOutlet weak var timerLabel: UILabel!
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 timerStartButton(_ sender: Any) {
var password_Text: UITextField?
let alertController = UIAlertController(title: "To start your own 2 Cocktails for £10 APPi Hour", message: "get a memeber of the team to enter the password, but use it wisely, as you can only use it once per day, with remember great power comes great responsability", preferredStyle: UIAlertControllerStyle.alert)
let tickoff_action = UIAlertAction(title: "let the APPiness commence", style: UIAlertActionStyle.default) {
action -> Void in
self.timerStartButton.isEnabled = false
Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(appiHour.enableButton), userInfo: nil, repeats: false)
if let password = password_Text?.text{
print("password = \(password)")
if password == "baruba151" {
self.counter = 60
self.timerLabel.text = String(self.counter)
self.timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(appiHour.updateCounter), userInfo: nil, repeats: true)
}
} else {
print("No password entered")
}
}
alertController.addTextField { (txtpassword) -> Void in
password_Text = txtpassword
password_Text!.isSecureTextEntry = true
password_Text!.placeholder = ""
}
alertController.addAction(tickoff_action)
self.present(alertController, animated: true, completion: nil)
}
@IBOutlet weak var timerStartButton: UIButton!
func updateCounter() {
counter -= 1
timerLabel.text = String(counter)
if counter == 0{
timer.invalidate()
counter = 0
}
}
}
作为第二个问题,可以在应用程序处于后台运行计时器吗?我知道对于Sat Nav,音乐应用程序等,苹果皱眉。但是有没有一种方法,其中保持计时器并在本地发送通知,让用户知道计时器已经结束?
提前感谢。
答案 0 :(得分:2)
我怀疑您的操作可能无法连接到您的按钮。我只是尝试了以下代码,没有任何问题。该按钮被禁用,然后在5秒后启用:
class ViewController: UIViewController {
@IBOutlet weak var myButton: UIButton!
@IBAction func ButtonPressed(_ sender: Any) {
myButton.isEnabled = false
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(myTimerTick), userInfo: nil, repeats: false)
}
func myTimerTick() {
myButton.isEnabled = true
}
}
因此,请确保您的插座和操作正确连接到按钮。如果右键单击按钮,则应在插座和操作旁边看到填充的点。您应该在代码中看到类似的填充点。
您可以通过在“timerStartButton”方法中放置断点并确保断点被击中来进一步验证它是否已连接。
编辑以进一步说明:您需要将代码连接到Interface构建对象。有关如何执行此操作的完整教程,请参阅此article from Apple。
答案 1 :(得分:0)
如果这是你的意思,我不能100%肯定。但这至少会满足您的请求的第一部分:在计时器运行时禁用按钮,并在计时器停止后重新启用。
pygame.sprite.Sprite
以下是您获得的几个屏幕截图。 它在用户启动时启用,在计数进行时禁用,然后恢复到原始状态,计数器为0且启用按钮。 这就是你想要的吗?
至于你的第二个问题,你是什么意思
@IBOutlet weak var myButton: UIButton!
@IBOutlet weak var timerCount: UILabel!
@IBAction func buttonPressed(_ sender: UIButton) {
var count = 0
sender.isEnabled = false
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [unowned self] timer in
count += 1
if count == 5 {
count = 0
sender.isEnabled = true
timer.invalidate()
}
self.timerCount.text = "\(count)"
}
}
您是否希望计时器在应用程序处于后台时继续运行,然后在计时器结束后更新用户?如果是这样,请看一下这个应该指向正确方向的答案:Continue countdown timer when app is running in background/suspended