How do I access my viewController from my appDelegate? iOS

后端 未结 6 2243
遇见更好的自我
遇见更好的自我 2020-11-28 05:31

I have an iOS app I created as a \"view-based app\" in xCode. I have only one viewController, but it is displayed automatically, and I see no code that ties it to my appDel

6条回答
  •  时光说笑
    2020-11-28 05:42

    Swift way to do it, you can call this from anywhere, not just appdelegate:

    /// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
    public var topMostVC: UIViewController? {
        var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
        while let pVC = presentedVC?.presentedViewController {
            presentedVC = pVC
        }
    
        if presentedVC == nil {
            print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.")
        }
        return presentedVC
    }
    

    Its included as a standard function in:

    https://github.com/goktugyil/EZSwiftExtensions

提交回复
热议问题