UIKeyboard notification handler triggered 3 times - ios

My code:
-(void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleKeyboard:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)handleKeyboard:(NSNotification*)notification {
NSLog(#"triggered");
}
See:
The disappearing handler is triggered once as normal, but 3 times when appears. Is this a iOS bug?

Possibly not helpful for your problem but you need to be calling [super viewWill… for your overrides.
-(void)viewWillAppear:(BOOL)animated
-(void)viewWillDisappear:(BOOL)animated
From the docs.
If you override this method, you must call super at some point in
your implementation.

Related

The NSNotificationCenter is never executed

This is my code.
Here create the observer to Notification called Example into ViewController
- (void)addObserverExample
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(example:)
name:#"Example"
object:nil];
}
- (void)example:(NSNotification *)notification{
NSLog(#"Example!!!");
}
From viewDidLoad register my observer
- (void)viewDidLoad
{
[self addObserverExample];
}
In my second ViewController. When tapped a button excute this code:
[[NSNotificationCenter defaultCenter] postNotificationName:#"Example" object:self.dictKeys userInfo:nil];
The problem I have is that the notification is never executed.
Any idea.
Have created demo for NSNotificationCenter as per your question and it's working fine for me. Here it is the link of that code: NSNotificationCenter Demo
- (void)viewDidLoad {
[super viewDidLoad];
[self addObserverExample];
}
- (void)addObserverExample
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(example:)
name:#"Example"
object:nil];
}
- (void)example:(NSNotification *)notification{
NSLog(#"Example!!!");
NSLog(#"%#",notification.userInfo);
}
- (IBAction)btnFireNotification:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"Example" object:nil userInfo:#{#"key" : #"value"}];
}
I believe the problem you're having may be related to the fact that in your second view controller, you're passing self.dictKeys in the object parameter.
If you want to pass data via the NSNotificationCenter, you should use the userInfo parameter instead.
Darshan's example does this the correct way.

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 calling two times

Below is what I have.
MainViewController.m
- (IBAction)sideMenuAction:(id)sender {
NSLog(#"login==sideMenuAction");
[[NSNotificationCenter defaultCenter] postNotificationName:#"ShowMySideMenuNotification" object:self];
}
NotificationListener.m
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ShowMySideMenuNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(adjustShowMenu) name:#"ShowMySideMenuNotification" object:nil];
}
-(void) adjustShowMenu {
NSLog(#"notification adjustShowMenu=");
}
Now when I click side menu button in MainViewController, what I was expecting is call adjustShowMenu from NotificationListener once, however it is called twice.
Below is the NSLog for the same.
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
2015-01-20 12:27:30.799 abc[699:169314] notification adjustShowMenu=
What I was expecting is
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
Any idea what is going wrong?
Note: I also tried in viewDidAppear instead of viewDidLoad, but its giving same result.
When I searched online, many answers asked to removeObserver. I did same, but still twice notification is getting called.
As per answer here, I make changes as below and its working fine now.
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(adjustShowMenu) name:#"ShowMySideMenuNotification" object:nil];
}
-(void) viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ShowMySideMenuNotification" object:nil];
}

Posting nsnotification but observer is not hearing it

I am using NSNotifiationCenter, like a 1000 other times, and post a notification, but my observers are not hearing it? What gives?
=== THIS IS IN MY TABLEVIEW ===
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSuccessSocket)
name:kNotificationServiceStartSuccessful
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleFailedSocketConnection)
name:kNotificationSocketFailedToConnect
object:self];
}
=== THIS IS IN MY SOCKETMANAGER (socket manager is a singleton if that matters) ===
-(void)willStartService {
....
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationServiceStartSuccessful object:nil];
....
}
I have debugged and viewDidLoad is being called in my view. Yes, my willStartService is being called.
You are registering the TableView only for notifications sent by itself. It should be..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSuccessSocket)
name:kNotificationServiceStartSuccessful
object:nil];
Try setting object:nil in your -addObserver:selector:name:object method calls. What you're doing is telling Notification Center to ONLY listen for notifications that come from the table view instance.
If you don't want to pass nil you'll have to pass an instance of your socket manager.
Check the Docs:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:

Showing a hidden button

Please bear with me, as I am still learning the ropes of objective c.
I currently have a button that when pressed hides the iOS keyboard. I was wondering how I could do the opposite of this. When a text field is selected, the keyboard automatically appears- at the same time, I'd like this button to also appear on screen.
Thanks!
-(IBAction)done:(id)sender{
//...
//...
[Screen resignFirstResponder];
done.hidden = YES;
};
You will have to use NSNotification. Implement the addObserver in viewWillAppear and removeObserver in viewWillDisappear
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
-(void)keyboardWillShow:(NSNotification*)notification{
done.hidden = NO;
}
register for the UIKeyboardWillShowNotification like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
and show the button in the method provided:
-(void) keyboardWillShow:(NSNotification*)notification{
done.hidden = NO;
}
do not forget to remove the observer in dealloc with
[[NSNotificationCenter defaultCenter] removeObserver:self];
You can use UITextFieldDelegate method to achieve this for example:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
done.hidden = NO;
return YES;
}
Hope it will help.

Resources