2017-03-08 82 views
0

我使用回調來開發BLEModule。 以下是檢查電源的簡單代碼。 它在ViewDidLoad中運行良好。 但是,爲什麼代碼在@IBAction中沒有響應?Swift - 點擊後回撥電話


class ViewController: UIViewController { 

    var ble = BLEManager.sharedInstance 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     ble.confirmPower { (result) in 
      print(result) //true or false 
     } 
    } 

    @IBAction func checkBtn(_ sender: Any) { 
     print("power") 
     ble.confirmPower { (result) in 
      print(result) //non-response 
     } 
    } 
} 

我使用回調開發BLEModule。 以下是檢查電源的簡單代碼。 它在ViewDidLoad中運行良好。 但是,爲什麼代碼在@IBAction中沒有響應?

+0

點擊了按鈕後,您是否驗證過「電源」打印到控制檯?如果沒有,你可能忘了將你的Storyboard中的按鈕連接到'ViewController'代碼中的'checkBtn(_ :)'方法。 – RobertJoseph

回答

0

centralManagerDidUpdateState觸發你的回調會被調用,相信centralManagerDidUpdateState將在應用程序啓動被解僱,所以在你的viewDidLoad回調會被調用。但是,當您按下按鈕時,centralManagerDidUpdateState未被觸發,因此不會調用回調。

+0

對不起,我犯了一個錯誤。我的代碼就像這樣的代碼。 –

+0

結果仍然相同。 –

+0

@ David.Choi你可以試試我的編輯? – chengsam

0

你應該檢查你的故事板,你的scannerBtn鏈接到正確的touchEvent,例如touchUpInside你可以通過右擊你的按鈕來做到這一點。多一點信息,以幫助解決您的問題,將不勝感激。

+0

感謝您的評論。我已經鏈接被檢查。打印代碼行被執行並且回調代碼行不會運行。 –