从AppDelegate调用委托函数不起作用 [英] Calling delegate function from AppDelegate not working
问题描述
我试图在AppDelegate中调用委托函数,但似乎永远不会被调用。
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.
}
}
代表总是没有,它永远不会进入内部功能。那我现在不是关于代表的话是什么? Google GIDSignInDelegate 及其委托函数如何从控制器类中调用AppDelegate?我知道这可能是一个非常愚蠢的问题,但我仍然想知道。谢谢
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
好的,因为我没有使用navigationController嵌入我的控制器。因此,如果让,它不会进入。它的工作原理如下 - :
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
}
推荐答案
您无法直接创建 Viewcontroller
对象并在<委托
code> app delegate 。您需要首先访问 Viewcontroller
对象,因为它位于 rootViewController
中,因此您需要像这样实现
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
}
这篇关于从AppDelegate调用委托函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!