touchesBegan EndEditing not working - ios

I have a number of textfields in a view, and a textView, what I am trying to do is to tab through those UI elements after editing stops and the element resigns the status as FirstResponder.
//Code to tap on empty area on screen so key board should disappear
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
//a text field should now be the first responder after text area should end editing
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[self.txtNumber becomeFirstResponder];
return YES;
}
Problem comes after the following steps
1. textView becomes first responder
2. tap outside textview resigns as first responder
3. txtNumber becomes first responder and numpad appears
4. tap outside.
now keyboard doesn't disappear

Use:
#implementation ViewController
NSNumber *tabCount;
-(void) viewDidLoad
{
tabCount = 0;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
if([tabCount integerValue] ==0)
[self.txt0 becomeFirstResponder];
if([tabCount integerValue] ==1)
{
[self.txt0 resignFirstResponder];
[self.txt1 becomeFirstResponder];
}
return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"])
tabCount = tabCount + 1;
}
#end
The same concept can be done for UITextField using:
-(void)textFieldDidBeginEditing:(UITextField *)textField
-(BOOL)textFieldShouldReturn:(UITextField*)textField

try following method
may be useful
if you are using present model view controller
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}

Related

keyboard not disappear when used resignFirstResponder in iOS

coding environment sierra 10.12,Xcode 8.1. Two textField in my roorView, if first textfield's text is nil, second textfield will can't be editing.When i used resignFirstResponder method to turn off keyboard in textFieldDidBeginEditing: method, keyboard not disappear. I add a fullscreen TapGesture in rootView. I'm very confused,anyone have ideas to help me deal with this problem?
`
#pragma mark -- UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == self.password) {
[self.username resignFirstResponder];
NSString *name = self.username.text;
if ([name isEqualToString:#""]) {
[CFBlurHUD showFaild:#"sign in error!"];
[self performSelector:#selector(dismmiss) withObject:nil afterDelay:1.5f];
self.password.enabled = NO;
}
}
}
- (void)dismmiss{
[CFBlurHUD dismiss];
self.password.enabled = YES;
}
`
If you want to simply dismiss a keyboard from view, try the following:
self.view.endEditing = true;
use your UITextFieldDelegate methods specifically UITextFieldShouldBeginEditing and return NO and execute the code to show the popover instead. This way the keyboard is never shown to begin with
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self.view endEditing:YES];
return NO;
}
Because current textfiled is password: if (textField == self.password) {. But you set:
[self.username resignFirstResponder];
It 's not correct.
Try:
[self.username resignFirstResponder];
[self.password resignFirstResponder];
or :
self.view.endEditting = true;
use your UITextFieldDelegate methods specifically UITextFieldShouldBeginEditing and return NO and execute the code to show the popover instead. This way the keyboard is never shown to begin with
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self.view endEditing:YES];
return NO;
}
First step for dismiss keyboard Give textfeild delegate in storyboard
Or Also give by code :
self.txtName.delegate = self;
Dismiss keyboard when tap gesture method called:
- (void)dismmiss{
[self.view endEditing:YES];
[CFBlurHUD dismiss];
self.password.enabled = YES;
}
or Also Dismiss when touch anywhere in View:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
Or when user press return button in keyboard:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

How to redraw the keyboard of a UITExtview when i change return key type when already displayed

In a UitTextview, i want the keyboard return key to operate like a newline key when user is typing, and i want the same key to turn to a Done key after few seconds of inactivity and dismiss the keyboard when touched instead of inserting a newline
I've modified the textview delegates to do the newline versus dismiss keyboard mechanism and to switch the keyboard return key from UIReturnKeyDefault to UIReturnKeyDone and the newline versus dismiss mechanism works great. the only problem i have is that when i change the return key type to self.TextView.returnKeyType = UIReturnKeyDone; the keyboard is not refresh/redrawn and i cannot see the done key. I've tried doing a setNeedsDisplay on the UITextView and on my main view, but nothing happen. The keyboard return key does not change . When the keyboard is dismissed and then reappear when user reenter the textview then the done key is here.
What should i do to have the done key being redrawn while keyboard is active and display ?
Here is the uitextview delegates i used:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
text_newline_mode=YES;
self.TextView.returnKeyType = UIReturnKeyDefault;
}
- (void)textViewDidChange:(UITextView *)txtView
{
text_newline_mode=YES;
self.TextView.returnKeyType = UIReturnKeyDefault;
if (keyboard_timer)
[keyboard_timer invalidate]; // cancel previous timer when user continue typing
keyboard_timer = [NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(enableKeyboardDoneKey:)
userInfo:nil
repeats:NO];
}
-(void)enableKeyboardDoneKey:(NSTimer *)timer
{
text_newline_mode=NO;
keyboard_timer = nil;
self.TextView.returnKeyType = UIReturnKeyDone;
[self.TextView setNeedsDisplay]; // this does not refresh the keyboard
[self.view setNeedsDisplay]; //this does not help neither
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if (text_newline_mode==YES)
{
self.TextView.text = [self.TextView.text stringByAppendingString:#"\n"];
return NO;
}
else return YES;
}
- (void)textViewDidEndEditing:(UITextView *)txtView
{
self.feedbackTextView.returnKeyType = UIReturnKeyDefault;
text_newline_mode=YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if(([text isEqualToString:#"\n"]) && (text_newline_mode==NO))
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
I looked everywhere but were not able to find any information on how to refresh the keyboard keys while keyboard is displayed.
Any ideas ?
thanks for your help
In my view, we can't change the returnKeyType once the keyboard is displayed. I have other alternate. Once check this.
Store the time when a key is entered initially, then compare it with the time whenever the new key is entered. Then, based upon your time delay, check your condition in
static NSDate *tempDate = [NSDate date];
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//Store the initial time
NSDate *dateEntered = [NSDate date];
NSTimeInterval interval = [dateEntered timeIntervalSinceDate:tempDate];
if (interval > 10.0f) //10.0 is your buffer time, after which you want to dismiss the keyboard
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
tempDate = dateEntered;
return YES;
}

UITextView not resigning

Ok I have exhausted google and this site looking for an answer and cannot find one. I currently have a UITextView (postMessage) and the keyboard for it will not disappear. I have tried every possible code combination to make it dismiss but it simply will not.
I have tried doing the return key method
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]){
//MainViewController *mvc = [[MainViewController alloc] init];
//[mvc.view endEditing:YES];
//[[self view] endEditing: YES];
//[self.postMessage becomeFirstResponder];
//[self.topLayer endEditing:YES];
//[self.view endEditing:YES];
//[self.collectionView endEditing:YES];
//[self.postMessage.editable = NO];
//[postMessage resignFirstResponder];
[self.postMessage resignFirstResponder];
return NO;
}else{
return YES;
}
}
I also have tried using a UIToolBar with a UIBarButton
-(IBAction)done:(id)sender {
[self keyboardIsHidden];
[[self view] endEditing: YES];
[self.postMessage becomeFirstResponder];
[self.topLayer endEditing:YES];
[self.view endEditing:YES];
[self.collectionView endEditing:YES];
//[self.postMessage.editable = NO];
[self.postMessage resignFirstResponder];
keyboardBar.hidden = YES;
NSLog(#"done");
}
And nothing..... I have used NSLogs to make sure that both methods are being called, but the keyboard will not resign. I have also use and if statement to check to make sure the UITextView (postMessage) is firstResponder and it is.
In my project I have 2 views (not view controllers) a bottom layer view and top layer. The user pulls the top layer up into view. I also have a UICollectionView.
I have noticed that when I press the return key on the keyboard or the 'Done' key on my UIToolBar the UICollectionView reloads the data.
I do have the UITextViewDelegate set on the .h.
I also have postMessage.delegate = self in my ViewDidLoad
Thanks!
-Mike
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#"\n"]) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
i am using this work fine
you can use like this.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
or create a button ..
-(IBAction)btnclicked{
[txtvw resignFirstResponder];
}
Attach your TextField Delegate with File owner , Clean build and run Project

How to change event of return key? [duplicate]

This question already has answers here:
How to navigate through textfields (Next / Done Buttons)
(35 answers)
Closed 9 years ago.
i have 4 text fields , i want to parform following actions by clicking on return key -
1- when i enter entry in one field and press return key , curser should be jumped on next
TextField .
Thanks .
Implement UITextField delegate and set tag value to all your textField in series. like 1,2,3..as so on. then, make sure scrollView and textField tag values should not conflict.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
int nextTextFieldTag = textField.tag+1;
UITextField *textFieldd = (UITextField*)[self.view viewWithTag:nextTextFieldTag];
if (textFieldd!=nil) {
if ([textField isKindOfClass:[UITextField class]]) {
[textFieldd becomeFirstResponder];
return NO;
}
}
return YES;
}
Try this
Take the 4 textfields as textfield1,textfield2,textfile3,textfield4 .
In the textfield delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if([textField isEqual:textfield1])
{
[textfield2 becomeFirstResponder];
return NO;
}
else if([textField isEqual:textfield2])
{
[textfield3 becomeFirstResponder];
return NO;
}
// Do for other textfields
return YES;
}
You just need to know what the next TextField should be and have UITextFieldDelegateProtocol especially the - (BOOL)textFieldShouldReturn:(UITextField *)textField implemented.
In - (BOOL)textFieldShouldReturn:(UITextField *)textField simply figure out the next TextField and do [nextTextField becomeFirstResponder];
So the - (BOOL)textFieldShouldReturn:(UITextField *)textField Method could look like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:firstTextField]) {
[secondTextField becomeFirstResponder];
}
//When last textfield dismiss the keyboard
else if ([textField lastTextField]) {
[textField resignFirstResponder];
}
return NO;
}

How to disable keyboard in UITextView or how make the return key work?

I am using UITextView without xib (programmatically). I have a text scrolling horizontally. I am not able to disable the keyboard, I've used:
textView.editable = NO;
Else I would like to use the return key which is also not working, I've used:
-(void)textFieldDidEndEditing:(UITextView *)textView{
NSLog(#"%#",textView.text);
[textView resignFirstResponder];
Or can I have IBAction Option which I could programmatically...
well i can tell something which works when someone touches anywhere after entering the data.
use this method
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan: touches withEvent:event];
[[self textView]resignFirstResponder];
}
here is a link which can which can help you to do the same using Delegation.
UITextView hide keyboard in iphone
To dismiss the keyboard when done button is pressed use this
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}

Resources