I have a class called game.h and it has an instance method called pause. How can I call this from app delegate when my game goes into the background?
I know that you use - (void)applicationWillResignActive:(UIApplication *)application, but I want to call pause on my existing instance.
Using NSNotificationCenter.
In your instance of the game class, add self as an observer to the UIApplicationWillResignActiveNotification notification.
In your game class, somewhere, you need the following snippet:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(pause)
name:UIApplicationWillResignActiveNotification
object:nil];
This should probably go in init.
This notification is fired by applicationWillResignActive. The addObserver:selector:name:object: method sets your object up to call the selector you tell it whenever it receives that notification.'
Don't forget to remove self as an observer in dealloc.
In game.m's #implementation:
- (void)dealloc {
// If not using ARC, then be sure to [super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Related
I have three viewControllers, and I'm trying to send a notification from viewController 3 to viewController 1 and 2. I think the best way to do this is to use NSNotification. Here's what I have so far:
In class C - Post the notification
[[NSNotificationCenter defaultCenter] postNotficationName:#"Updated "object:self];
In class B
In class A and B - Register first for the notification
// viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleUpdate:) name:#"Updated" object:nil];
-(void)handleUpdate:(NSNotification *)notification {
NSLog(#"recieved");
}
This works so far. But when I de-register it in class A and B:
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
The handleUpdate method doesn't get called. So the obvious problem is when I removeObserver's for the notification.
My question is, if everything I did so far is correct, why isn't it working when I remove the removeObserver? If it's not correct, where can I removeObserver's?
Everything you did is right. this is how the notification work.
If your class A,B always need to handle the update, you won't removeObserver.
Because you add your "addObserver" in viewDidLoad. it means you addObserver only once.
The normal error is that you add "addObserver" in "viewWillAppear" or "viewDidAppear", it will add more than once observer in the class. Then, you have to removeObserver in viewDidDisappear.
I want to call a method from another class via NSNotificationCenter.Everything is working fine.
The problem is my method called up two times.
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(removeAllSubViews:) name:#"getTheRequest" object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)removeAllSubViews:(NSNotification *)notification
{
NSLog(#"%#",notification.object);
NSLog(#"Print");
}
ViewController2.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] postNotificationName:#"getTheRequest" object:#"mySite"];
// Do any additional setup after loading the view.
}
When I run, I get this in console:
Why my method is called up two times ?
Edit
When I use this code in ViewController2.m it works fine. But Why?? **
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"getTheRequest" object:nil];
You have to remove observer after use of it inside the method.
- (void)removeAllSubViews:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver("getTheRequest")]
NSLog(#"%#",notification.object);
NSLog(#"Print");
}
I've seen this before when I had a retain cycle in my view controller, so every time a instance of this view controller was created, it was added as an NSNotificationCenter observer, but because of the retain cycle when the view controller was dismissed, it was never actually deallocated/released from memory, so technically it was still an observer.
You might want to try to add the following to your view controller:
- (void)dealloc {
NSLog(#"Dealloc called.");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
This should remove your view controller as an observer when it is dismissed, if there is no retain cycle, and if there is the NSLog will never be called - which is indicative of a larger memory-related issue, where what you're seeing is just a side-effect.
You are probably seeing multiple calls to that method, because you register the observer multiple times (e.g. you have navigated to that view controller before), but did not remove it again at the appropriate places. Anyways, viewDidLoad is very likely not the ideal place to register an observer. A common place to do this is the designated initializer, and removing it again in dealloc.
As a side note (and without seeing enough code for a very informed opinion), your use case ("remove all subviews") does not sound like notifications are a good approach. Have you considered using delegation?
It is possible that there is a double NSNotificationCenter registration.
I think you have declared another:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(removeAllSubViews:) name:#"getTheRequest" object:nil];
somewhere.. trying finding the other one..
And just a tip, when you register for NSNoticationCenter try removing first the observer like:
// removes the observer
[[NSNotificationCenter defaultCenter] removeObserver:YourObserver name:YourName object:YourObject];
followed by:
// register
[[NSNotificationCenter defaultCenter] addObserver:YourObserver selector:YourSelector name:YourName object:YourObject];
Just to remove the existing one, if any..
There's a way to notify a object when the method "applicationDidEnterBackground" of application delegate is called but just getting the applicationObject.
I need to do some action when application get on background but I just have access to the application object through "[UIApplication sharedApplication]".
Note: I need those 3 methods applicationWillTerminate, applicationWillEnterForeground, applicationDidEnterBackground but I can't access to applicationDelegate methods.
You can use NSNotificationCenter to inform your class that these methods are being called.
In the init register the the correct notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminateNotification:) name:UIApplicationWillTerminateNotification object:nil];
This will call a method this method, which you will have to add in you class:
- (void)applicationWillTerminateNotification::(NSNotification *)notifictaion{
}
The notification that you want to add are: UIApplicationWillTerminateNotification, UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification
Don't forget to unregistered the you class instance in the dealloc of you class, even in ARC:
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
}
I had the similar problem. My solution was to get the instance of AppDelegate and register my object as observer:
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate addApplicationDidEnterBackgroundObserver:myobject];
// and later...
[appDelegate removeObserver:myobject];
sure the solution by rckoenes is more elegant.
Currently I'm only calling one method when application will enter foreground. How do I call various methods in #selector?
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(displayHappyFace)
name:UIApplicationWillEnterForegroundNotification
object:nil];
Just create a separate function for all your other function.
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(AllFunction)
name:UIApplicationWillEnterForegroundNotification
object:nil];
All functions.
-(void) AllFunction
{
[self displayHappyFace];
[self otherFunction];
}
Add another observer to UIApplicationWillEnterForegroundNotification if you wish to keep the methods' logic separate:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(displayHappyFace)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(callOtherMethod)
name:UIApplicationWillEnterForegroundNotification
object:nil];
#selector supports only one method. Remember to remove self as the observer before releasing its memory, to avoid messages being passed to a nil object.
You can only put one selector there.
Best practice is to create a new method called handleNotificationName: for each notification.
Example:
- (void)handleUIApplicationWillEnterForegroundNotification:(NSNotification *)aUIApplicationWillEnterForegroundNotification { }
This makes it really easy to figure out where your app handles each notification and makes code maintenance easy.
Inside the handler method you can call whatever methods you need to. You can have conditional logic also based on state you have or based the userInfo dictionary of the Notification ( if it has one ).
Don't forget to remove your notification observer in you object's dealloc method (at least, if not somewhere else because you might not want to always receive the notification depending on the use case)
App delegate:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:APP_REFRESH_NOTIFICATION object:nil];
}
In my view controller:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doStuff) postNotificationName:APP_REFRESH_NOTIFICATION object:self];
}
- (void)doStuff never gets called. Why?
I assume that you've typed your question incorrectly and you'd meant to write addObserver:selector:name:object:, instead of addObserver:selector: postNotificationName:object: (such method doesn't exist).
In the documentation of - (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
we can read:
notificationSender
The object whose notifications the observer wants
to receive; that is, only notifications sent by this sender are
delivered to the observer. If you pass nil, the notification center
doesn’t use a notification’s sender to decide whether to deliver it to
the observer.
So in your case, as you're passing object:nil in postNotificationName:object:, you also have to set object:nil in addObserver:selector:name:object:.
According to the documentation you also should replace the method doStuff with:
- (void)doStuff:(NSNotification *)notification
and use #selector(doStuff:) in addObserver:selector:name:object:.
You're passing self as the object parameter to addObserver:selector:name:object:, but doStuff doesn't accept any parameters, so the method call fails (silently). Your viewDidLoad should look like this:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doStuff)
name:APP_REFRESH_NOTIFICATION
object:nil];
}
You're app delegate is posting a notification when the app becomes active, but your view controller isn't subscribing to that until its view gets loaded. If your app delegate is creating your view controller and loading it (which is probable) then your controller doesn't even exist at the time the notification is posted, which is why it isn't receiving it. If you use a storyboard, and that controller is the entry point in the storyboard, AND you use the info.plist for your app to set that storyboard as the main interface, then it will have already instantiated the controller and loaded its view by the time -applicationDidBecomeActive: is called, solving your problem.