現在、私のMainViewControllerはBluetoothモジュールに接続し、そこからのデータを読み取ることができます。 今、別のView Controllerからデータを読み込もうとしています。2のUIViewControllerでBLEデバイスの値を読み込み
マイBluetoothマネージャは、複数回インスタンス化されないようにシングルトンです。適切なViewControllerのデータを読み込んで処理するために、私はオプションのデリゲートを使用することを考えていました。
を私は次のエラーを取得する:
[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(データを追加する場合:私がreceivedMVC(データ::文字列)を取得するときに正常に動作しています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'を忘れてしまったので、見つからない/クラッシュしてしまいました。 はい、私は 'delegate?.receivedUVC?(data:stringValue)'を使うべきです ありがとうございます:) – downuts