I have a UIView which contains UITexView and a Button.
I have a delegate UITextViewDelegate.
When I first set the cursor on the UITextView, the delegate function "textViewShouldEndEditing" gets called, and this function triggers the UIKeyboardWillShowNotification notification. So far so good.
When I click on the button, I call the function [self.textView resignFirstResponder];, this function then calls the delegate "textViewShouldEndEditing", but the notification UIKeyboardWillHideNotification never gets called.
I have a listener for the notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Is there anything missing ???
[self.view endEditing:YES]; is the trick, Thank you all for your help
you should post a notification to the notification center in the following way
[[NSNotificationCenter defaultCenter]
postNotificationName:#"TestNotification"
object:self];
and you can check whether the notification has received or not in the following way
if ([[notification name] isEqualToString:#"TestNotification"])
NSLog (#"Successfully received the test notification!");
Is your code is like this, check and let me know
-(void)viewDidAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void) keyboardWillHide:(NSNotification *)note{
NSLog(#"test");
}
Related
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.
I have a xib file with custom view on which I have many textfields subviews. I have set delegates on each of the textfield to the file owner and successfully able to texfield delegate methods. But unfortunately the keyboard notification methods keyboardWillShow: and keyboardWillHide: not being called at all.
I added observers at textfieldShouldBeginEditing and removed the observers at textFieldDidEndEditing.
Here is the snippet of my code:
Adding Observer
-(void) textFieldShouldBeginEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
Removing observer
-(void) textFieldDidEndEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//Methods for notification
- (void)keyboardWillShow:(NSNotification *)notification
{
NSLog(#"Here");
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSLog(#"There");
}
//Any of those keyboardwillShow and keyboardWillHide is not called.
It will be great if someone can help me figure out the issue with my code or any underlaying issue which I may be missing.
You are adding an observer when you are showing keyboard by editing textView. It won't call because keyboard is already shown. You should add observers on your viewWillAppear and remove viewWillDisappear methods.
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];
}
Notification only called the function when observer_ViewController is active
AppDelegate.m
[[NSNotificationCenter defaultCenter] postNotificationName:kNewShipNotifaction object:ship];
observer_ViewController.m
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newShipCome:) name:kNewShipNotifaction object:nil];
}
- (void)newShipCome:(NSNotification *)notifacation{
[self updateNotifyWithShip:notifacation.object];
}
Notification didn't call the newShipCome:(NSNotification *)notification method when I'm in another viewController. When I switched to the observer_ViewController, the method still didn't get called.
So...how can I get notification update correctly when I'm not in the observer_ViewController ?
remove de-register code form viewWillDisappear it will work
Hi is there any way to detect iPad keyboard hiding button ? i mean when user press this button :
something going to happen !
I'm not sure what you want to accomplish, but maybe this can help you: Register with NSNotificationCenter to receive the UIKeyboardWillHideNotification and/or UIKeyboardDidHideNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myKeyboardWillHideHandler:)
name:UIKeyboardWillHideNotification
object:nil];
...
- (void) myKeyboardWillHideHandler:(NSNotification *)notification {
NSLog(#"Keyboard wants to hide. What a coward.");
}
put this to viewDidLoad
// register to track event when user presses hide keyboard button on bottom right cornor for iPAD
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textFieldShouldReturn:) name:UIKeyboardWillHideNotification object:nil];
and this will make your - (BOOL)textFieldShouldReturn:(UITextField *)textField; delegate method to get called when keyboard down button is pressed in iPAD.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textFieldShouldReturn:) name:UIKeyboardWillHideNotification object:nil];
This actually crashes on the go.
But if you call a custom method, like:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myCustomeMethodToResignTextFieldResponder) name:UIKeyboardWillHideNotification object:nil];
Then it will work just fine.. :-)
with Javascript
I found a workaroud for iPad IOS7. I will test on IOS8 to make sure it works. So basically I create a listener on every FOCUSOUT event (for all my texts) and I call my function.
It fires when you have your keyboard open and when you close your "keyboard". It doesn't fire when you select another text field or button, because it targets on null. If you use in combination with keydown, you can save multiple value and call your submit function only when you release your keyboard.
document.addEventListener('focusout', function(e) {
if (e.relatedTarget == null){
alert("close keyboard without click on something else");
callYourFunction();
}
});