尋找一些幫助,這一點,我已經提交密碼,這是偉大之後工作的計時器,但後來我需要計時開始後禁用按鈕和一個被禁用的時間期間,(在代碼我已經進入了一個標稱90秒)禁用計時器按鈕確認斯威夫特 - 不工作
然而按鈕不禁用。
,如果有人能告訴我在哪裏,我錯了,這將是真棒。
import UIKit
類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
}
}
}
作爲次要問題是有可能運行計時器而應用程序是在後臺?我知道蘋果在這方面對於衛星導航,音樂應用等方面不滿。但是有沒有一種方法可以讓定時器保持,並且在本地發送通知讓用戶知道定時器已經結束?
在此先感謝。
我在任何地方都看不到「self.timerStartButton.isEnabled = false」。我懷疑這就是爲什麼你沒有讓你的按鈕被禁用。至於在後臺運行計時器,是和否。您可以阻止iOS終止您的應用長達3分鐘。查找「UIApplication.shared.beginBackgroundTask()」。但是,您將無法永久運行計時器。最終,iOS會迫使你的應用程序進入睡眠狀態。 – JacobJ
由於我看不到Interface Builder佈局,因此我假設您有兩個動作連接到同一個按鈕的TouchUpInside事件。 (Start_Button和timerStartButton)。這很好,但你需要確保兩者都連接到你的按鈕。右鍵點擊你的按鈕,並確保你看到兩個都適當地連接起來。 – JacobJ
是啊,剛纔注意到,第二個動作不應該在那裏,因爲只需要一個按鈕動作,我已經替換了它應該是的代碼,但它現在崩潰了,因爲計時器在驗證後啓動。任何「self.timerStartButton.isEnabled = false」應該去的建議? –