2017-06-22 10 views
1

現在、私の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 
} 

答えて

0

[BLE_Tests.MainViewController **receivedUVCWithData:]

これは、MainViewControllerメソッドのreceivedUVCWithData:が呼び出されたが、そのクラスがそれを実装していないことを示しています。それは呼ばれている理由と理由のthats:

delegate?.receivedUVC!(data: stringValue) 

この呼び出しは、デリゲートが存在するかどうかを確認しますので、それがreceivedUVCにメッセージを送信する場合に存在しなければなりません(!)。

delegate?.receivedUVC?(data: stringValue) 

をしかし、あなたはあなたのプロトコルに2つの異なる方法を定義し、なぜ、私は自分自身に尋ねる:あなたはこれを呼び出すのであれば、それはクラッシュしないのだろうか?プロトコルに必須の(オプションではない)メソッドを1つ定義し、両方のUIViewControllerで実装し、BluetoothManager.swiftでこのメソッドを呼び出します。次に、最後に設定されたデリゲートがデータを取得します。両方のViewControllerが同時に存在する場合、BluetoothManagerにはdelegate1とdelegate2が必要で、両方のデリゲートでメソッドへの呼び出しが必要です。

+0

愚かな私!私はUpdateViewControllerのViewDidLoadで 'bluetoothManager.delegate = self'を忘れてしまったので、見つからない/クラッシュしてしまいました。 はい、私は 'delegate?.receivedUVC?(data:stringValue)'を使うべきです ありがとうございます:) – downuts

関連する問題