I was trying to call an existing method of my ViewController.m
from AppDelegate.m
inside the applicationDidEnterBackground
method,
Background information (class definition vs class instance)
The important concept here is the difference between a class definition and a class instance.
The class definition is the source code for the class. For example ViewController.m
contains the definition for the myViewController
class, and AppDelegate.m
contains the definition for the AppDelegate
class. The other class mentioned in your question is UIApplication
. That is a system-defined class, i.e. you don't have the source code for that class.
A class instance is a chunk of memory on the heap, and a pointer to that memory. A class instance is typically created with a line of code like this
myClass *foo = [[myClass alloc] init];
Note that alloc
reserves space on the heap for the class, and then init
sets the initial values for the variables/properties of the class. A pointer to the instance is then stored in foo
.
When your application starts, the following sequence of events occurs (roughly speaking):
delegate
in the UIApplication instanceThe storage of the pointer to MyViewController is where things get messy. The AppDelegate class has a UIWindow property called window
. (You can see that in AppDelegate.h.) If the app only has one view controller, then the pointer to that view controller is stored in the window.rootViewController
property. But if the app has multiple view controllers (under a UINavigationController or a UITabBarController) then things get complicated.
The spaghetti code solution
So the issue that you face is this: when the system calls the applicationDidEnterBackground
method, how do you get the pointer to the view controller? Well, technically, the app delegate has a pointer to the view controller somewhere under the window
property, but there's no easy way to get that pointer (assuming the app has more than one view controller).
The other thread suggested a spaghetti code approach to the problem. (Note that the spaghetti code approach was suggested only because the OP in that other thread didn't want to do things correctly with notifications.) Here's how the spaghetti code works
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.myViewController = self;
This code retrieves the pointer to the UIApplication instance that the system created, and then queries the delegate
property to get a pointer to the AppDelegate instance. The pointer to self
, which is a pointer to the MyViewController instance, is then stored in a property in the AppDelegate.
The pointer to the MyViewController instance can then be used when the system calls applicationDidEnterBackground
.
The correct solution
The correct solution is to use notifications (as in kkumpavat's answer)
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)didEnterBackground
{
NSLog( @"Entering background now" );
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
With notifications, you aren't storing redundant pointers to your view controllers, and you don't have to figure out where the system has stored the pointer to your view controller. By calling addObserver
for the UIApplicationDidEnterBackgroundNotification
you're telling the system to call the view controller's didEnterBackground
method directly.