2016-11-16 147 views
2

尋找一些幫助,這一點,我已經提交密碼,這是偉大之後工作的計時器,但後來我需要計時開始後禁用按鈕和一個被禁用的時間期間,(在代碼我已經進入了一個標稱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 



    } 


} 

}

作爲次要問題是有可能運行計時器而應用程序是在後臺?我知道蘋果在這方面對於衛星導航,音樂應用等方面不滿。但是有沒有一種方法可以讓定時器保持,並且在本地發送通知讓用戶知道定時器已經結束?

在此先感謝。

+0

我在任何地方都看不到「self.timerStartButton.isEnabled = false」。我懷疑這就是爲什麼你沒有讓你的按鈕被禁用。至於在後臺運行計時器,是和否。您可以阻止iOS終止您的應用長達3分鐘。查找「UIApplication.shared.beginBackgroundTask()」。但是,您將無法永久運行計時器。最終,iOS會迫使你的應用程序進入睡眠狀態。 – JacobJ

+0

由於我看不到Interface Builder佈局,因此我假設您有兩個動作連接到同一個按鈕的TouchUpInside事件。 (Start_Button和timerStartButton)。這很好,但你需要確保兩者都連接到你的按鈕。右鍵點擊你的按鈕,並確保你看到兩個都適當地連接起來。 – JacobJ

+0

是啊,剛纔注意到,第二個動作不應該在那裏,因爲只需要一個按鈕動作,我已經替換了它應該是的代碼,但它現在崩潰了,因爲計時器在驗證後啓動。任何「self.timerStartButton.isEnabled = false」應該去的建議? –

回答

1

我懷疑你的行爲可能不被掛接到您的按鈕。我只是嘗試下面的代碼沒有問題。該按鈕被禁用,然後允許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」的方法,並確保斷點時迷上了。

編輯,以進一步澄清:您需要將代碼連接到您的界面構建對象。請參閱article from Apple以獲取有關如何操作的完整教程。

enter image description here

+0

感謝您排序的問題引用了,而不是標籤!現在所有的工作 –

0

我不是100%肯定,如果這是你的意思。但是這至少可以滿足您的第一部分請求:在定時器運行時禁用按鈕,並在定時器停止時重新啓用它

@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)" 
    } 
} 

下面是你得到的一些截圖。 用戶啓動時啓用,在計數進行時禁用,然後在計數器爲0且按鈕啓用時恢復到其原始狀態。 這就是你要去的地方嗎?

enter image description here enter image description here

至於你的第二個問題,你是什麼意思

the timer is held 

你希望計時器繼續運行,而應用程序是在後臺,然後更新用戶一旦計時器已過?如果是這樣,看看這個答案應該指向正確的方向:Continue countdown timer when app is running in background/suspended

+0

實質上是的,但如果他們把應用程序帶回前臺,它會顯示計時器調整後的當前值。即如果它在一個小時開始後在35分鐘後移到背景,則它被帶回到前面,並且定時器將顯示25分鐘。 –