简体   繁体   English

在Swift中没有通过委托调用函数

[英]Function not being called through delegate in Swift

I can't get the app to execute changeCard() after abc() finishes executing. 在abc()完成执行之后,我无法使应用程序执行changeCard()。 The two functions are in different classes. 这两个函数位于不同的类中。 Here is the code below. 这是下面的代码。 Any help is appreciated! 任何帮助表示赞赏!

protocol NetworkControllerDelegate {
    func changeCard()
}

class NetworkController {
    var networkControllerDelegate: NetworkControllerDelegate?

    func abc () {
    //do something here
    networkControllerDelegate?.changeCard()
    }
}

class View: UIViewController, NetworkControllerDelegate {
    var networkController = NetworkController()

    func changeCard() {
    //do something here
    networkController.networkControllerDelegate = self
    }
}

I've read all the similar questions on stackoverflow for the past few hours but still can't figure it out. 在过去的几个小时中,我已经阅读了关于stackoverflow的所有类似问题,但仍然无法解决。 Thanks! 谢谢!

Try moving the following line into viewDidLoad of your view controller so that the delegate is set up prior to use. 尝试将以下行移动到视图控制器的viewDidLoad中,以便在使用之前设置委托。

networkController.networkControllerDelegate = self

Also, you might want to think about making the networkControllerDelegate variable in NetworkController weak so as to avoid any retain cycles. 另外,您可能需要考虑使NetworkController中的networkControllerDelegate变量变弱,以避免任何保留周期。

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

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