简体   繁体   中英

Function not being called through delegate in Swift

I can't get the app to execute changeCard() after abc() finishes executing. 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. Thanks!

Try moving the following line into viewDidLoad of your view controller so that the delegate is set up prior to use.

networkController.networkControllerDelegate = self

Also, you might want to think about making the networkControllerDelegate variable in NetworkController weak so as to avoid any retain cycles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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