简体   繁体   English

Delegate 在 Swift 程序中为零

[英]Delegate is nil in a Swift program

I'm practising how to communicate between two view controllers using protocol and delegate (in the xCode background even when I'm using protocol in my project I get the same problem, Delegate is nil), but the problem after setting everything it shows me that my delegate is nil and the sender VC does not send any data since the delegate is nil.我正在练习如何使用协议和委托在两个视图控制器之间进行通信(在 xCode 背景中,即使我在我的项目中使用协议,我也遇到了同样的问题,委托为零),但是设置所有内容后的问题显示给我我的委托为零,发送方 VC 不发送任何数据,因为委托为零。

I have confirmed to the protocol and I have set the receiver VC as the delegate, but still can not see where the problem is.我已经确认了协议并将接收方VC设置为delegate,但仍然看不出问题在哪里。

The Protocol议定书

protocol theCommunicationsStructionProtocol{

func dataToTransmit(Data: String)

}

The Sender VC发件人 VC

class TheSenderVC{

var delegate: theCommunicationsStructionProtocol?

func lookingForDelegate(){


self.delegate?.dataToTransmit(Data: "Data has been sent")

 }
}

The Receiver VC接收器 VC

class TheReceiverVc1: theCommunicationsStructionProtocol{
var TheSenderVCObj = TheSenderVC()

func delegateLuncher(){

TheSenderVCObj.delegate = self
}

func dataToTransmit(Data: String) {
print("from VC1: \(Data)")
 }

}

calling delegateLuncher() to set the delegate in the receiver VC调用 delegateLuncher() 在接收方 VC 中设置委托

TheSenderVC().lookingForDelegate()

calling lookingForDelegate() from the sender VC to look for the delegate and send it the data从发送方 VC 调用lookingForDelegate() 以查找委托并将数据发送给它

TheReceiverVc1().delegateLuncher()

Note: I have tried accessing the delegate from the receiver VC using this way:注意:我尝试使用这种方式从接收方 VC 访问委托:

class TheReceiverVc1: theCommunicationsStructionProtocol{
 var TheSenderVCObj: TheSenderVC?

func delegateLuncher(){

self.TheSenderVCObj?.delegate = self
}

func dataToTransmit(Data: String) {
print("from VC1: \(Data)")
 }

}  

but I still getting the same problem:但我仍然遇到同样的问题:

delegate is nil代表为零

Finally, I found the solution!终于,我找到了解决方案! the problem is I was making instances of the TheSenderVC, instead of takling to the original TheSenderVC.问题是我正在制作 TheSenderVC 的实例,而不是处理原始的 TheSenderVC。 when I was making an object (an instance) of TheSenderVC the problem occurred!当我制作 TheSenderVC 的对象(一个实例)时,问题发生了! instead I have to access the original TheSenderVC, which means I have to use static :)相反,我必须访问原始的 TheSenderVC,这意味着我必须使用静态 :)

here is the old delegate set up这是旧的代表设置

var delegate: theCommunicationsStructionProtocol?

from TheSenderVC来自 TheSenderVC

here is the new delegate set up这是新的代表设置

static var delegate: theCommunicationsStructionProtocol?

therefore the因此

func lookingForDelegate(){


self.delegate?.dataToTransmit(Data: "Data has been sent")

}

will be changed to将更改为

static func lookingForDelegate(){


self.delegate?.dataToTransmit(Data: "Data has been sent")

}

since now it includes a static property (delegate)因为现在它包含一个静态属性(委托)

on the other hand, the The ReceiverVC1 should be changed from:另一方面,ReceiverVC1 应该从:

class TheReceiverVc1: theCommunicationsStructionProtocol{
var TheSenderVCObj = TheSenderVC()

func delegateLuncher(){

TheSenderVCObj.delegate = self
}

func dataToTransmit(Data: String) {
print("from VC1: \(Data)")
   }

  }

to:到:

class TheReceiverVc1: theCommunicationsStructionProtocol{


func delegateLuncher(){

TheSenderVC.delegate = self
}

func dataToTransmit(Data: String) {
print("from VC1: \(Data)")
}

}

accessing the delegate from the original TheSenderVC()从原始 TheSenderVC() 访问委托

Where are you create the reference of TheSenderVCObj你在哪里创建TheSenderVCObj的引用

replace var TheSenderVCObj: TheSenderVC?替换var TheSenderVCObj: TheSenderVC? to var TheSenderVCObj = TheSenderVC()var TheSenderVCObj = TheSenderVC()

let try below code:让我们试试下面的代码:

class TheReceiverVc1: theCommunicationsStructionProtocol{
 var TheSenderVCObj = TheSenderVC()

func delegateLuncher(){

self.TheSenderVCObj?.delegate = self
}

func dataToTransmit(Data: String) {
print("from VC1: \(Data)")
 }

}  

your TheSenderVCObj also nil according to your code.根据您的代码,您的 TheSenderVCObj 也为零。

NOTE: use proper naming conventions.注意:使用正确的命名约定。

Because the TheReceiverVc1 was automatic deinit by ARC .因为TheReceiverVc1是由ARC自动取消初始化的。 You need to save reference of the instance like that's:您需要像这样保存实例的引用:

class ViewController: UIViewController {
    let theReceiverVc1: TheReceiverVc1 = TheReceiverVc1()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        theReceiverVc1.delegateLuncher()
    }
}

Also make sure when you using delegate set it as weak var:还要确保在使用委托时将其设置为弱变量:

weak var delegate: theCommunicationsStructionProtocol?

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

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