Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 364
Andy Avatar asked Mar 21 '15 03:03

Andy


People also ask

What is difference between AppDelegate and scene delegate?

AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.

How do I get Viewcontroller from view?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

What is AppDelegate file in iOS?

In iOS 12 and earlier, you use your app delegate to manage major life cycle events in your app. Specifically, you use methods of the app delegate to update the state of your app when it enters the foreground or moves to the background.


1 Answers

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.

like image 140
Mundi Avatar answered Sep 20 '22 02:09

Mundi