2017-08-06 15 views
0

AppDelegateで代理関数を呼び出そうとしていますが、呼び出されないようです。AppDelegateから代理関数を呼び出せません

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. 
    } 


} 

デリゲートは常にnilであるが、それは関数内で行くことはありません。何が私はまだ 代議員についてはありませんか? GoogleはどのようにしてGIDSignInDelegateとその代理関数をコントローラクラスからAppDelegate内で呼び出すのですか?私はそれが非常にばかな質問かもしれないことを知っていますが、私はまだ知りたいです。ありがとう

私はnavigationControllerで私のコントローラを埋め込んでいないので、それは働いた。それでとすれば、に入っていなかった。それは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

あなたのapp delegate内部delegateを直接Viewcontrollerオブジェクトを作成し、設定することはできません。それはあなたがあなたが共有アプリケーションからAppDelegateインスタンスを取得しようとすることができ、この

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

まだスウィフトある

役立つことを願っています。 –

+0

@ TusharSharma、私はデリゲートの値を見ることができ、それは私のために無しではありません –

+0

ViewControllerクラスはどこにあなたのストーリーボードの中で定義しましたか? Main.storyboardの中にありますか? –

1

のように実装する必要がrootViewController内側にあるので、あなたは最初Viewcontrollerオブジェクトにアクセスする必要があります。それはこれは私が今code.Issueを更新せた場合、それは内部つもりはないですnilを3

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

     delegate?.callfromDelegte(indicator: indicator) 
} 
+0

ありがとう。 –

関連する問題