简体   繁体   中英

IOS Swift, delegate to communicate between two view controllers

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM