I have a customized UITableViewCell with textfields. The textfields of the cells are set to call for delegate functions.
Inside
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
if(textField == fromTF){
fromTF.text = [[[fromTF.text substringToIndex:2] stringByAppendingString:#":"] stringByAppendingString:[fromTF.text substringFromIndex:2]];
[toTF becomeFirstResponder];
return YES;
}
if(textField == toTF){
[toTF resignFirstResponder];
[intTF becomeFirstResponder];
return YES;
}
return YES;
}
This is delegate method is called in my custom cell.However when called, the UIKeyBoardWillHideNotification addobserver object is not removed when pressed 'return' key. Is there a way I can resolve this?
Try like this
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
and also check this link textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2
it may help you.
Hello Ganesh thank you for the answer. I removed the resignFirstResponder and passed the firstResponder directly to the next textfield. This prevented the keyboard from disappearing.
Related
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.
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.
When i touch a label UIMenuController appears with my custom list of items, and that works well. But when UIMenuController appear in a standard UISearchBar i see all my custom items there too. Why?
I need to show only standard (copy, paste) items for standard UISearchBar and custom items when i touch the label. Can you explain how should i do that?
UPDATE
What i did (not good solution):
If we have keyboard, that is searchbar, if we don't, that is label.
Flag, which mean which items list i will use
BOOL standardList;
Register for keyboard appear/disappear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[super viewWillDisappear:animated];
}
-(void)keyboardWillShow:(id)sender
{
standardList = YES;
}
-(void)keyboardWillHide:(id)sender
{
standardList = NO;
}
And check flag, in next method:
- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender
{
if (selector == #selector(copy:))
{
return YES;
}
if (!standardList)
{
if ((selector == #selector(makeCall:)) || (selector == #selector(createNewContact:)))
{
return YES;
}
}
return NO;
}
That work well, BUT: iPad keyboard have "hide keyboard" button, and keyboard can hide without [UISearchBar resingFirstResponder].
Even if i add next lines:
-(void)keyboardWillHide:(id)sender
{
standardList = NO;
[mySearchController setActive:NO];
}
that's still bad solution - i can't hide keyboard while searching and scroll search results.
Any suggestions?
One solution:
u can always reset the UIMenuItems after u customize UIMenuController.
addObserver of UIMenuControllerWillHideMenuNotification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willHideEditMenu:) name:UIMenuControllerWillHideMenuNotification object:nil];
then in the willHideEditMenu: function
-(void)willHideEditMenu:(NSNotification *)object
{
//any other thing u want to do.
[[UIMenuController shareMenuController] setMenuItems:nil];
}
this way, UIMenuController singleton in other places won't get affected by what u have done where u customize it.
Other solution:
i think the root reason why u have this problem is u have some function with the same name of "makeCall:" or "createNewContact:" in other view or viewcontroller (basically may in any UIResponder) that is the parent view (viewcontroler) of the place where u have this problem.
so check out the responder tree, see if u can find any selectors with the same name of the UIMenuItem selectors. since
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender;
is a function of any UIResponder, it could get called. and if it does happen to have a selector with the same name, it could return YES, and u will have this problem. To solve the problem, just rename the selector of the UIMenuItem.
My English isn't good. Hope u get my point and try it.
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.
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");
}