使用委托方法在 2 个类之间发送数据
Posted
技术标签:
【中文标题】使用委托方法在 2 个类之间发送数据【英文标题】:Sending data between 2 classes with Delegate Methods 【发布时间】:2019-08-14 11:54:41 【问题描述】:我正在尝试通过 MultipeerConnectivity 在 2 个类之间发送数据。我正在尝试通过委托方法来做到这一点。
我已经定义了这个协议:
protocol AssetDelegate
func changeThisName(data: Data)
我的主要 ViewController 是 AssetDelegate,并简单地实现了该功能:
func changeThisName(data: Data)
print("changeThisName called")
if let image = UIImage(data: data)
imageViewAsset.image = image
这只是为了查看接收到的图像是否可以被解码并分配给 imageViewAsset UI 元素。
此方法在我定义的 MultipeerConnectivity“Manager”类中调用,该类在 AppDelegate 中有一个实例(这是“singleton”的定义吗?)
我的“MPCManager”类有各种 MCSession 委托方法,特别是 session(didReceive:data) 方法,即:
var assetDelegate: AssetDelegate?
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID)
print("Data Recieved: \(data)")
self.assetDelegate?.changeThisName(data: data)
我在控制台日志中看到了print("Data Received:(data)")
。但是我没有看到 changeThisName 方法的打印语句。
这是控制台日志输出。基本上有 2 行显示 MPC 连接(“其他状态”和“会话连接”)。然后有 2 个“Received Data”项。
2019-08-14 12:42:29.714402+0100 My App[34860:11895498] [Accessibility] ****************** Loading GAX Client Bundle ****************
2019-08-14 12:42:44.121481+0100 My App[34860:11895563] [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
other state...<MCPeerID: 0x281b541c0 DisplayName = SendDevice>
2019-08-14 12:42:45.493567+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.542647+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.586389+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.641138+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.730210+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.795905+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.968786+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
session connected... <MCPeerID: 0x281b541c0 DisplayName = SendDevice>
2019-08-14 12:42:46.037399+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:46.379153+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:46.444389+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:47.099950+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:47.158868+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:48.351072+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:48.390215+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:49.856339+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:49.922462+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
Data Recieved: 8269 bytes
2019-08-14 12:42:51.375239+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:51.454734+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:52.913337+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:52.977319+0100 My App[34860:11895661] [ERROR] ProcessEvent:1199 Send BINDING_REQUEST failed(C01A0041).
Data Recieved: 4569624 bytes
2019-08-14 12:42:54.271102+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [0].
2019-08-14 12:42:54.293615+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [1].
2019-08-14 12:42:54.306191+0100 My App[34860:11895531] [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.306387+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [2].
2019-08-14 12:42:54.313382+0100 My App[34860:11895531] [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.313444+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [3].
2019-08-14 12:42:54.318396+0100 My App[34860:11895531] [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.318453+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [5].
2019-08-14 12:42:54.323359+0100 My App[34860:11895531] [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.323407+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [6].
2019-08-14 12:42:54.328377+0100 My App[34860:11895531] [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
【问题讨论】:
【参考方案1】:protocol AssetDelegate
func changeThisName(data: Data)
class A: AssetDelegate
func goToClassB()
var b = B()
b.assetDelegate = self // <-- I don't see this line in your code
class B
var assetDelegate: AssetDelegate?
func passDataToA()
self.assetDelegate?.changeThisName(whatEverDataHere)
【讨论】:
谢谢。我对哪个类需要包含哪些代码感到困惑……在您的示例中,“A 类”是将数据发送到(接收者)的位置?就我而言,我认为这是我的主要 ViewController 类,它还包含我的 imageView 实例的 IBOutlet。但是我应该为你的var b = B()
提供什么类型的实例?是 MPCManager 类(管理 Multipeer 连接)吗,因为这是发出“发送”请求/changeThisName(data)
方法调用的类?
获得了基本功能!让我失望的是创建一个“B 类”的实例。就我而言,这是我的自定义 MPCManager 类,我的应用程序有一个在 AppDelegate 中调用的实例(我想知道它是否是“单例”)并且已经存在......所以在我的主 ViewController 的 viewDidLoad() 函数中,我添加了appDelegate.mpcManager.assetDelegate = self
,重新运行,现在调用委托方法并在接收到图像时在 imageView 中显示图像(还必须将此 UI 更新放到主 GCD 线程上)以上是关于使用委托方法在 2 个类之间发送数据的主要内容,如果未能解决你的问题,请参考以下文章
使用委托从 UIView 子类向 UIViewController 发送数据