Reputation: 8323
I am interested how other developers draw the line between AppDelegate and ViewController. I would like to know how others have handled it and not just my specific case. Examples would be appreciated.
I have reached a point where the notifications of the AppDelegate are needed in the ViewController. In my case, the current ViewController needs to know when the app is deactivate or terminated.
Thanks in advance
Upvotes: 0
Views: 247
Reputation: 23223
View Controllers manage a set of views (or now possibly a set of other view controllers also). Anything going beyond the realm of managing your views don't really belong in the view controller. Many of us bend this rule by adding simple UITableViewDelegates
and UITableViewDataSources
in a view controller; or other similar code.
The AppDelegate is the delegate for the whole application. It should be handling application level events. When you app gets backgrounded, you might need to save state for data handled throughout the application.
There is nothing wrong with a UIViewController
needing notifications of event which the AppDelegate receives and handles also. The application delegate should application wide needs, but the view controller might have a specific need. Just take your view controller and register for the event you want (it doesn't matter if the app delegate also handles the exact same event) and then do whatever you need to do in order to manage your views properly.
Upvotes: 1
Reputation: 20163
I avoid using the App Delegate for everything possible. It's too easy to use it as a sort of global-variable crutch, which leads to poor design. In your particular case, you can get access to those notifications anywhere in your app (via NSNotificationCenter) without having to use the App Delegate:
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
Upvotes: 2