I'm trying to hide the keyboard after the user clicked on the return button of the keyboard.
I'm using this function to hide it:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
When the textview is empty this function works but once there is a characters in the textview,nothing happens and the keyboard doesnt get hidden.
I would suggest to use [self endEditing:YES]; or self.view endEditing:YES]
Found the answer. Was my fault. I had a function which once the user finished editing i'm checking if it's empty or not. For some reason returned no instead of yes. Thanks for the help
Related
I have a UITextView. I have the delegate for myTextView set to self and, when I do normal editing, this method calls just fine:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(#"Called");
}
In my app, I call in my code: [myTextView insertText:#"Hello World"];. When I do, I need to call textView:shouldChangeTextInRange:replacementText: after the text is inserted. How do I do this?
Call it explicitly. Call it before editing and only perform the edit if it returns YES. To call it explicitly you need to know the selected range (get the selected range with selectedTextRange), that's it. You already have the text to add and the text view.
Thanks for the answer, #Wain!
Here's what worked:
- (void)insertText
{
NSString *stringToAdd = #"Hello World";
NSString *replacementText = [myTextView.text stringByAppendingString:stringToAdd];
[napkinTextView insertText:stringToAdd];
[self textView:myTextView shouldChangeTextInRange:NSMakeRange(0, stringToAdd.length) replacementText:replacementText];
}
I'm using Storyboard with this project and have a UITextField inside a view. The user is able to type in a search term, press return and a segue to a resultsViewController occurs as intended.
The problem I am having is that if for any reason the keyboard gets dismissed, the segue occurs automatically. For example, if the user taps the iPad's drop keyboard key, the segue occurs without a search term... or if the user taps outside the UITextField, the keyboard drops (as intended), but the segue also occurs (not intended).
Here's are the methods I'm using (the UITextField's delegate is set in storyboard); also, I've put in "resignFirstResponder" and "endEditing: YES" messages in several places as I was trying to figure out a solution. Sorry for the mess:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField.text isEqualToString:#""]) {
[textField resignFirstResponder];
return NO;
}
self.clueString = textField.text;
[textField resignFirstResponder];
return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.view resignFirstResponder];
return YES;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
Well, I feel silly but I'll answer my question in case anyone else has this problem.
To control when the segue should or should not occur, I needed to implement the following method:
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([self.searchField.text isEqualToString:#""]) {
return NO;
}
return YES;
}
Have you tried setting this method to always return no? You might need to have a check in there on whether or not to search. (Like your above method)
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.view resignFirstResponder];
return YES;
}
Before you dismiss my question as being a duplicate of this one: iphone, dismiss keyboard when touching outside of UITextField
The issue for me is that my textfield is part of my prototype cell which has its own subclass of uitableview cell and so I'm not sure how to reference the textfield when trying to resign the first responder. SO, I can't just do this:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
How would I get across this situation?
Thanks
Try this code, it's very useful:
-(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event
{
[aTextField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
I think dismissing keyboard on tapping outside the table view cell is not good. User may accidently touching outside or he can scroll while entering text right?. You can use the UITextFieldDelegate to dissmiss keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]
}
If I got you right you want to resign keyboard wile tapping on outSide of textfield but you don't have reference of your textfield.
Try this;
Take global textField, lets call it reftextField
Now in textFieldDidBeginEditing set referenced text field to
- (void) textFieldDidBeginEditing:(UITextField *)textField{
reftextField = textField;
}
Now you can happily use on any button clock, (adding a transparent button on begin editing recomended)
- (void)dismissKeyboard {
[reftextField resignFirstResponder];
}
Or for resigning done button try this.
//for resigning on done button
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
I hava a UITextView ,and after some characters' input, I want to use a button click to hide the keyboard and the UITextView is still in focus....[Attention:the button is not a return key or a done key];
Have you set delegate to your UITextView instance?
If so, try using [_textView resignFirstResponder];
else
Try using [self.view endEditing:YES];
mplement following code to your view controller .m file & .h file add delegate of textView
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}
Now goto interface builder, select your textview & set return key type done.
I've a textview. I want to know whether the changes done in textview while pressing back button in UINavigationBackbutton. how to compare old and new text entered in UItextview?
If there is any changes, i'll ask Do u want to save the changes?.
Just set a flag when your UITextView did begin editing (wasEdited=YES) and save the current state of the text (originalText = myTextView.text) then on backbutton check the (originalText isEqualToString:myTextView.text && wasEdited)
The was edited tag is to avoid string comparaison in case the user didn't get into editing :)
iphony,
Better late than never...
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(#"textViewShouldBeginEditing");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"textViewDidBeginEditing");
}
- (void)textViewDidChange:(UITextView *)textView {
NSLog(#"textViewDidChange");
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSLog(#"textViewDidChangeSelection");
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(#"textViewShouldEndEditing");
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
NSLog(#"textViewDidEndEditing");
}