Search code examples
swiftviewcontrollerappdelegatedelegation

update viewcontroller from appdelegate - best practice?


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?


Solution

  • I think the second solution is much easier. It also ensures the view controller does not go out of memory (is "released") and the app delegate does not try to contact an inexistent object.

    Also, I do not understand the delegate argument in the case of more than one controllers. In that case, you would have to have multiple delegates, really not an ideal situation. Or you would have to change the delegate to different view controllers from the outside - also not very elegant and quite risky.

    For the multiple view controller scenario I recommend notifications via NSNotificationCenter. This is a lose coupling that ensures only those objects that need to react receive the message. View controllers should start listening to the notifications upon creation and deregister from the notification center when they disappear.