Receive NSNotification method not calling - ios

I have use below for for register notification:-
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllSearch:) name:filter object:nil];
-(void)AllSearch:(NSNotification *)notification
{
NSLog(#"abc");
}
and i post notification with this code:-
[[NSNotificationCenter defaultCenter] postNotificationName:filter object:nil];
But "AllSearch" Method not calling.

plz check you code you have to addObserver first
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllSearch:) name:filter object:nil];
befor postNotification
[[NSNotificationCenter defaultCenter] postNotificationName:filter object:nil];

Related

Observer in UITableviewCell

In my custom UITableViewCell I added an observer in NSNotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopActivityIndicator) name:#"stopActivityIndicator" object:nil];
I post a notification in a UIViewController:
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil];
This function "stopActivityIndicator" is not being called, any idea what causes this?
EDIT:
ExploreViewController.m
-(void)showCorrectBannerAfterPlusButtonClicked:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil];
}
ExploreViewController contains a UITableView with ExploreTableViewCells.
ExploreTableViewCell.m
- (IBAction)plusButtonClicked:(id)sender
{
self.plusButton.hidden = YES;
[self.plusButtonActivityIndicator startAnimating];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(stopActivityIndicator) name:#"stopActivityIndicator" object:nil];
}
-(void)stopActivityIndicator
{
_plusButton.hidden = NO;
[self.plusButtonActivityIndicator stopAnimating];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"stopActivityIndicator" object:nil];
}
Ok, question, when do you call "showCorrectBannerAfterPlusButtonClicked"?? As this is the method where you post the notification observed by the tableViewCell.
What I see is the notification observer adding and removal, but I don't see how the UIViewController knows when the cell's "plusButtonClicked" is called, so perhaps the method posting the notification is not being called.
Also, be careful with the cell reusage, have in mind if you should remove the observer at that point.

NSNotificationCenter Thread breaks at second call even after it has been removed

I am using NSNotificationCenter for two different ViewControllers. First time calling NSNotificationCenter works well and after that I remove the observer. But if I run it again, the thread breaks even after removing the observer.
in ViewController1:
[[NSNotificationCenter defaultCenter] postNotificationName:#"textUpdateNotification" object: nil ];
in ViewController2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:#"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
I tried with removing the observer in - (void)dealloc there also the same thing is happening.
Try removing the observer in viewDidDisappear
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
Sorry for my typo,
I am removing observer in the source ViewController where I am triggering the NSNotification, I should remove it from second ViewController where its being used.
in ViewController1:
[[NSNotificationCenter defaultCenter] postNotificationName:#"textUpdateNotification" object: nil ];
in ViewController2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:#"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
Thanks for every one

UIMoviePlayerControllerDidExitFullscreenNotification NSNotification not working in iOs 8

[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeFinished:) name:#"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
UIMoviePlayerControllerDidExitFullscreenNotification NSNotification not working in iOs 8
i am also faced the same problem but finally ended with using UIWindowDidBecomeVisibleNotification and UIWindowDidBecomeHiddenNotification, for embedded youtube videos(assuming by looking at your selector method names)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullScreen:) name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullScreen:) name:UIWindowDidBecomeHiddenNotification object:nil];
above is a patch work around,(hope this helps)
I use MPMoviePlayerController and it works fine. I guess that depending what you are using it for, you don't need UIMoviePlayerController.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(youTubeFinished)
name:MPMoviePlayerDidExitFullscreenNotification
object:self.player.moviePlayer];

Using instance methods in the app delegate

I wanted to run a instance method from my BPGameController class called "pauseGame" in my app delegate when my application enters background mode and when it resigns, and a instance method called "resumeGame" when the application becomes active again, I've tried a few different things but none have worked for me so far
In BPGameController's viewDidLoad method sign up for the notifications as follows:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseGame)
name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumeGame)
name:UIApplicationDidBecomeActiveNotification object:nil];
When you are done remove yourself from observing.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

multiple notifications in MPMoviePlayerController

Is it possible to have two different playback notifications in a MPMoviePlayerController?
I want to use these:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(MPMoviePlayerPlaybackDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];
i did manage to get them working but when i use them together only MPMoviePlayerPlaybackStateDidChangeNotification is called.
Please advise.

Resources