目前,我的MainViewController可以连接到我的蓝牙模块并读取来自它的数据。 现在,我试图从另一个View Controller读取数据。从2个UIViewControllers的BLE设备读取值
我的蓝牙管理器是一个单身,所以它不会被多次实例化。为了阅读和处理适当的ViewController中的数据,我想使用可选的委托。它的正常工作,当我到达receivedMVC(数据:字符串)前往receivedUVC时,但崩溃(数据:字符串)
我收到以下错误:
[BLE_Tests.MainViewController receivedUVCWithData:]: unrecognized selector sent to instance 0x100d0a9d0 2017-06-22 16:25:58.634682-0700 BLE_Tests[9544:2692419] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BLE_Tests.MainViewController **receivedUVCWithData:]: unrecognized selector sent to instance 0x100d0a9d0'
如果我添加receivedUVC(数据:字符串)到我的MainViewController,它不会崩溃,但不会从正确的ViewController调用receivedUVC。
如何指向正确的选择器?
谢谢。
MainViewController.swift
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, BluetoothDelegate {
@IBOutlet weak var peripheralListTableView: UITableView!
@IBOutlet weak var updateButton: UIButton!
let bluetoothManager = BluetoothManager.getInstance()
override func viewDidLoad() {
super.viewDidLoad()
peripheralListTableView.delegate = self
peripheralListTableView.dataSource = self
bluetoothManager.delegate = self
bluetoothManager.discover()
}
func reloadPeripheralList() {
peripheralListTableView.reloadData()
}
func receivedMVC(data: String) {
print("Hello? \(data)")
}
//MARK: - UITableViewDataSource
}
UpdateViewController.swift
class UpdateViewController: UIViewController, BluetoothDelegate {
let bluetoothManager = BluetoothManager.getInstance()
func receivedUVC(data: String) {
print("Allo: \(data)")
}
}
BluetoothManager.swift
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
let stringValue = String(data: characteristic.value!, encoding: String.Encoding.utf8)!
print("Received packet: \(stringValue)")
delegate?.receivedMVC!(data: stringValue) // Delegate method in MainViewController
delegate?.receivedUVC!(data: stringValue) // Delegate method in UpdateViewController
}
傻逼我!我在我的UpdateViewController的ViewDidLoad中忘记了'bluetoothManager.delegate = self',这就是为什么它从未发现它/崩溃。 是的,我应该使用委托?.receivedUVC?(data:stringValue)' 非常感谢:) – downuts