简体   繁体   English

iOS Swift,委托在两个视图控制器之间进行通信

[英]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. 由于我不能跳回第二视图控制器,因此无法使用segues,因此决定尝试使用委托进行通信。

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 现在收到错误“ ViewController”类型的值,在mapViewController.delegate = self行没有成员“ delegate”

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. 好的,从注释中我们可以得知您没有设置delegate属性,从问题中我们知道您正在使用segues。 In order to set the property you need to override one method in your ViewController (the first one) : 为了设置属性,您需要重写ViewController一个方法(第一个方法):

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. 当然,这假定您的第二个VC名为SecondViewController如果没有,则相应地更改该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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