I am playing around with iBeacons implementing CoreLocation methods inside AppDelegate.swift (methods are implemented in AppDelegate to ensure App Background Capabilities)
In a SingleView application that comes with "ViewController.swift", what would be the best practice to pass data from AppDelegate to ViewController to update Views, say UIImages, UILabels or UITableViews ?
I have successfully implemented two different approaches:
1) Delegation, by firing Viewcontroller Delegation methods inside AppDelegate.swift:
protocol BeaconLocationDelegate { func minorBeaconChanged(minorValue:NSNumber) }
var locationDelegate: BeaconLocationDelegate
locationDelegate?.minorBeaconChanged(nearestBeacon.minor)
ViewController.swift:
in viewDidLoad method:
(UIApplication.sharedApplication().delegate as AppDelegate).locationDelegate = self
(I find this to look rather ugly - any better way to declare this property as delegate?)
protocol implementation:
func minorBeaconChanged(minorValue: NSNumber) {
// do fancy stuff here
}
2) By creating a reference to ViewController inside AppDelegate:
let viewController:ViewController = window!.rootViewController as ViewController
viewController.doSomethingFancy()
Both approaches work fine for me, but I think the first approach via delegation is more elegant and the better choice once you have multiple ViewControllers.
Any recommendations?