0
votes

My app has a main view controller where info from a bluetooth device is shown. To connect to the bluetooth device I segue to a different view controller to connect to the wanted bluetooth device. I then subscribe to that bluetooth device which calls a function every time the bluetooth device sends data.

After selecting which device I want to connect to, I segue back to my main screen where I want to show the data I'm receiving. I can see that the events from receiving packets from the bluetooth device are still working after switching back to the main screen with prints in the secondary view controller code.

My problem is that I want to take this data received in my second view controller and send it to the main view controller every time I receive it. Since I can't use segues because I don't jump back to the secondary view controller I decided to try to use delegate to communicate.

Here's what I've added so far in second view controller, the one sending the data:

In secondary view controller , before the class :

protocol sendLatLonDelegate : class{
   func sendReceiveData(data:AnyObject?)
}

At the top of my secondary view controller class with my other variables

weak var delegate:sendLatLonDelegate?

In the function that is called when I receive a packet from my bluetooth device

delegate?.sendReceiveData(latFloat)

Here's what I added in my main view controller, the one receiving the data:

Added this to the class definition

class ViewController: UIViewController,sendLatLonDelegate {

And inside my class added

 func sendReceiveData(data:AnyObject?) {       
    print("received data")
}

I'm printing the received data value before trying to send it to the main view controller and can see it correctly. I do not see my received data print though.

It's probably a small link I'm not realizing I'm missing between the two but I can't seem to find what it is. Anybody has suggestions? Thanks a lot!

Edit: From recommendation in answers, added

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
    if (segue.identifier == "ConnectDeviceSegue")
    {
        let mapViewController = segue.destinationViewController as!  ViewController
        mapViewController.delegate = self

    }
}

Now getting the error Value of type 'ViewController' has no member 'delegate' at line mapViewController.delegate = self

1
Well, did you set the receiving VC as the delegate? - Losiowaty
@Losiowaty Your second controller is not receiving any delegate value and going nil.you need to send delegate from first controller to second controller. - Tushar Sharma
@Losiowaty I did not, so I guess that's probably what is missing. Could you give me an example of where and how I should do that? From briefly looking I think I need to get an instance of of the receiving VC in my sending VC and set as the delegate there but I'm not so sure how to go about it. Thanks! - lhbortho
You can implement prepare for segue method and do it. - Tushar Sharma

1 Answers

1
votes

Ok, so from comments we got that you didn't set the delegate property, and from the question we know that you are using segues. In order to set the property you need to override one method in your ViewController (the first one) :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "ConnectDeviceSegue") {
        let mapViewController = segue.destinationViewController as!  DeviceJoinViewController
        mapViewController.delegate = self

     }
}

This of course assumes, that your second VC is named SecondViewController - if not, change that part accordingly.