Get Instance of Viewcontroller from Appdelegate in Swift

Get Instance Of ViewController From AppDelegate In Swift

You could try:

let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows") as! DetailViewController
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)

Swift 3:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = mainStoryboard.instantiateViewController(withIdentifier: "viewController")
self.present(viewController, animated: true, completion: nil)

Access current instance of view controller from app delegate in swift?

You have two options, the second one is better by design.

First option: (what you want)

I don't know the structure of your view controllers, so let me assume you have a root view controller, you could get it from AppDelegate via:

rootVC = self.window?.rootViewController

And if you want to get the presented view controller from the root view controller (like many apps, the presented view controller is a tab bar controller):

guard let tabBarController = rootVC.presentedViewController as? TabBarController else {
return
}

Once you get your tab bar controller, you can find the view controller in the array of view controllers:

tabBarController.viewControllers

Essentially, what I'm trying to say is you have to jump through your view controllers starting from the root to get to the controller you want, then grab the variable from there. This is very error prone, and generally not recommended.

Second option (better practice):

Have your view controller register as an observer for the UIApplicationWillResignActiveNotification notification. This will allow you to do whatever you want from the view controller when your app is about to enter background.

How to access ViewController variable from AppDelegate

You need get the rootViewController

func applicationDidEnterBackground(_ application: UIApplication) {

let viewController = self.window?.rootViewController as? ViewController
print(viewController?.animal)

}

Programmatically get instance of view controller

UITabBarControllers are most often used as applications' rootViewController. While you are not using storyboards, after app launch, if you are setting an instance of TabsController as your app's window's rootViewController, you can refer to this instance from anywhere in your app like so:

if let tabsController = UIApplication.sharedApplication().delegate?.window??.rootViewController as? TabsController {
tabsController.selectedIndex = 2
}

Access method in viewcontroller from appdelegate

You need to give your applicationDelegate class a reference to the viewController that you want to call the method on. You could do this by creating an instance variable or a property of the applicationDelegate that points to your viewController that you want to be able to call the method on. If you create your viewController in the init method of your appDelegate, or in your applicationDidLoad: method, then you can simply assign this viewController to the instance variable/property that you've created.

Passing an object to a ViewController from AppDelegate

  • In AppDelegate create a property

    private weak var viewController : ViewController?
  • In the view controller in viewDidLoad assign self to that property

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.viewController = self

Now you can access the view controller from AppDelegate

Connect to ViewController from AppDelegate (Swift)

It seems that AppDelegate can connect to objects only within Application Scene in a storyboard. If you want to get a ViewController, instantiate it from a storyboard.

sample:

@IBAction func menuAction(sender: AnyObject) {
if let storyboard = NSStoryboard(name: "Main", bundle: nil) {
let controller = storyboard.instantiateControllerWithIdentifier("VC1") as NSViewController

if let window = NSApplication.sharedApplication().mainWindow {
window.contentViewController = controller // just swap
}
}
}

How do I get a reference to the app delegate in Swift?

The other solution is correct in that it will get you a reference to the application's delegate, but this will not allow you to access any methods or variables added by your subclass of UIApplication, like your managed object context. To resolve this, simply downcast to "AppDelegate" or what ever your UIApplication subclass happens to be called. In Swift 3, 4 & 5, this is done as follows:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let aVariable = appDelegate.someVariable


Related Topics



Leave a reply



Submit