2017-03-08 81 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

感谢您的评论。我已经链接被检查。打印代码行被执行并且回调代码行不会运行。 –