2016-12-09 138 views
-3

我試圖禁用一定的時間,但有一個問題的按鈕。我的步驟如下:Swift:如何禁用按鈕

  1. 我有3個按鈕,所有按鈕都啓用一個按鈕被點擊
  2. 後,禁用所有按鈕。同時,通過藍牙發送數據...
  3. 完成發送數據後啓用所有按鈕。

我的目標是防止在通過藍牙發送數據時點擊按鈕。我嘗試使用Button.userInteractionEnabled = falseButton.enabled = false,但是每當我在完成發送數據後啓用按鈕時,它將再次進入按鈕動作處理程序(我在數據發送期間按下的處理程序)。是否有人知道如何永久禁用按鈕一段時間?

+0

你是說點擊的處理,但有延遲? (當使用'isEnabled = false'時。) –

+5

這裏需要的實際代碼。 – matt

回答

0

你需要做的是點擊時關閉按鈕,然後以某種方式啓用它時,數據傳輸完成。

如果此數據傳輸異步調用,它可能有一個參數,您可以在一個完成塊派:

button.isUserInteractionEnabled = false 
sendData(data) { 
    success in 
    button.isUserInteractionEnabled = true 
} 

如果不接受完成塊作爲參數,它可能會以不同的方式工作,如使用通知(射擊的通知具有特定名稱):

button.isUserInteractionEnabled = false 
sendData(data) 

// adding the observer that will watch for the fired notification 
NotificationCenter.default.addObserver(self, selector: #selector(self.didFinishSendingData(_:)), name: Notification.Name(rawValue: "NOTIFICATION NAME GOES HERE"), object: nil) 

func didFinishSendingData(_ notification: Notification?) { 
    button.isUserInteractionEnabled = true 
} 

如果您發佈代碼示例,我們絕對可以提供更多幫助。

0

爲什麼你在main threadactivityIndicator象下面這樣實現這一點:

let activityIndicator = UIActivityIndicatorView() 
    activityIndicator.frame = view.frame 
    activityIndicator.center = view.center 
    activityIndicator.activityIndicatorViewStyle = .gray 
    activityIndicator.hidesWhenStopped = true 
    view.addSubview(activityIndicator) 

    //start activity indicator 
    activityIndicator.startAnimating() 

    //send your data via bluetooth on main thread 
    DispatchQueue.main.async { 
     //put your sending via bluetooth code here with a completion handler when completes 
     //then in the completion handler, put below line 
     activityIndicator.stopAnimating() 
    }