iOS Swift,委托在两个视图控制器之间进行通信 [英] IOS Swift, delegate to communicate between two view controllers

查看:51
本文介绍了iOS Swift,委托在两个视图控制器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个主视图控制器,其中显示了来自蓝牙设备的信息.要连接到蓝牙设备,我选择连接到另一个视图控制器以连接到所需的蓝牙设备.然后,我订阅该蓝牙设备,该蓝牙设备每次在蓝牙设备发送数据时都会调用一个函数.

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.

我的问题是,我想接收在我的第二个视图控制器中接收到的数据,并在每次接收到它时将其发送给主视图控制器.由于我不能跳到第二视图控制器而不能使用segues,因此决定尝试使用委托进行通信.

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:

已将其添加到类定义中

class ViewController: UIViewController,sendLatLonDelegate {

在我的班级中添加

 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!

根据答案中的建议,添加了

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

    }
}

现在得到错误"map"类型的值在mapViewController.delegate = self行没有成员"delegate"

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

推荐答案

好吧,因此从注释中我们得知您没有设置 delegate 属性,从问题中我们知道您是使用segues.为了设置属性,您需要在 ViewController (第一个方法)中覆盖一个方法:

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

     }
}

这当然假定您的第二个VC名为 SecondViewController -如果没有,请相应地更改该部分.

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

这篇关于iOS Swift,委托在两个视图控制器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆