Reputation: 798
I'm writing a simple OS X app which so far is structured like so:
AppDelegate.m
ViewControllers
with xibs that the AppDelegate
owns and presents in a windowI sometimes need to access properties on AppDelegate from the ViewControllers, which I'm doing like this:
AppDelegate *delegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
delegate.someProperty = someValue;
This "feels" hacky. Is it a proper pattern or is this really dumb?
Two alternative architectures I came up with were:
The first feels tangled and dirty, the second feels unreliable.
What is the proper pattern here? How should VCs owned by the AppDelegate
access its properties?
Upvotes: 0
Views: 434
Reputation: 374
Try this
AppDelegate *app = [NSApp delegate];
Instead of this
AppDelegate *delegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
Upvotes: 1
Reputation: 7381
You can define a category on NSObject
that will allow you to get your instance of the AppDelegate
from any object in your project.
@interface NSObject (AppDelegate)
- (AppDelegate *)appDelegate;
@end
You can then include the category in your project .pch file so that it's available everywhere.
Upvotes: 0