In my application, a Connection
object is initialized in my AppDelegate.swift
, applicationDidBecomeActive()
.
How do I pass that Connection
object to my ViewController
subclass?
In my application, a Connection
object is initialized in my AppDelegate.swift
, applicationDidBecomeActive()
.
How do I pass that Connection
object to my ViewController
subclass?
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
Instances like Connection
are usually required throughout the application so the better idea is to create a shared instance of the required object and use anywhere we need.
class Connection{
static let sharedConnection = Connection()
....
}
usage:
let connection = Connection.sharedConnection