Calling delegate function from AppDelegate not wor

2019-03-05 10:18发布

问题:

I am trying to call delegate function in AppDelegate, but seems like it never get invoked.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,appdelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if let navigationController = window?.rootViewController, let
            viewController = navigationController.childViewControllers.first as? ViewController {
            viewController.delegate = self
        }
        // Override point for customization after application launch.
        return true
    }

    func callfromDelegte(indicator: UIActivityIndicatorView) {
        indicator.stopAnimating()
    }

ViewController-:

import UIKit

protocol appdelegate:class {
    func callfromDelegte(indicator:UIActivityIndicatorView)
}
class ViewController: UIViewController {

    @IBOutlet weak var indicator: UIActivityIndicatorView!
   weak var delegate:appdelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        indicator.startAnimating()
        indicator.hidesWhenStopped = true
    }

    @IBAction func rotateAction(_ sender: UIButton) {
        if delegate != nil{
        delegate?.callfromDelegte(indicator: indicator)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Delegate is always nil, it never goes inside function. What is that i don't now about delegates yet? How does Google GIDSignInDelegate and its delegate functions get called inside AppDelegate from controller class? I know it might be very stupid question but I would still like to know.Thanks

Ok it worked as i have not embeded my controller with navigationController. So it was not going inside if let. It worked simply like this-:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//        if let navigationController = window?.rootViewController, let
//            viewController = navigationController.childViewControllers.first as? ViewController {
//            viewController.delegate = self
//        }
        // Override point for customization after application launch.

        let controller = window?.rootViewController as! ViewController
        controller.delegate = self
        return true
    }

回答1:

You cannot create the Viewcontroller object directly and set the delegate inside your app delegate. You need to access the Viewcontroller object first because it is the inside the rootViewController so you need to implement like this

if let navigationController = window?.rootViewController, let 
   viewController = navigationController.childViewControllers.first as? ViewController {
 viewController.delegate = self
}


回答2:

You can try to get AppDelegate instance from shared application. Hope it will help

This is Swift 3

@IBAction func rotateAction(_ sender: UIButton) {
        let delegate = UIApplication.shared.delegate as? AppDelegate

        delegate?.callfromDelegte(indicator: indicator)
}