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;
}
Related
I have more than one text field in my view controller how can i navigate from one to another using the return button in the IOS keyboard.
Use UITextFieldDelegate delegate and the textFieldShouldReturn: method. Inside you can get the tag of the textfield passed in argument and direct to another one based on this information.
To have the delegate working you have to set delegate property of sender (textfield) to be the receiver (e.g. your view controller)
myTextField.delegate = self;
or do it in the storyboard (here is some storyboard hints). Your view controller then needs to specify this delegate as follows (in the "h" file):
#interface MyViewController:UIViewController<UITextFieldDelegate>
#end
and then in the "m" file
-(BOOL)textFieldShouldReturn:(UITextField*)textfield{
if([textfield tag] == 1)
{
//pass focus to next textfield
[self.nextTextField becomeFirstResponder];
} else {
//remove focus from current textfield
[textfield resignFirstResponder];//
}
return YES;//YES if textfield should implement its default behaviour
}
-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
[textField resignFirstResponder];
if(textField == _your1stTextField)
[_your2ndTextField becomeFirstResponder];
return YES;
}
try this...
set delegates to all of your text fields and then..
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else if textField == textField3)
{
[textField4 becomeFirstResponder];
}
.
.
.
[textField resignFirstResponder];
return YES;
}
I think this solution is more dynamic as it works for any number of textFields as long as your superview doesn't have a view with a tag == lastTextField.tag + 1
In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField1.delegate = self;
self.textField2.delegate = self;
...
self.lastTextField.delegate = self;
self.textField1.tag = 1;
self.textField2.tag = 2;
...
self.lastTextField.tag = n;
}
Then implement the textFieldDelegate:
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder)
[nextResponder becomeFirstResponder];
else
[textField resignFirstResponder];
return NO;
}
You need to declare at .h
.m set
yourTextfieldRef1.delegate=self;
yourTextfieldRef2.delegate=self;
yourTextfieldRef3.delegate=self;
yourTextfieldRef4.delegate=self;
yourTextfieldRef5.delegate=self;
.
.
.
set
yourTextFieldRef1.tag=1;
yourTextFieldRef2.tag=2;
yourTextFieldRef3.tag=3;
yourTextFieldRef4.tag=4;
yourTextFieldRef5.tag=5;
.
.
.
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return YES;
}
Hope it helps you...!
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;
}
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
I am trying to hide the keyboard in an iOS app. I've spent several hours looking for it, and I've tried pretty much everything, so I'm quite desperate.
My code follows as next:
RNViewController.h
#interface RNViewController : UIViewController <UITextFieldDelegate> {
UITextField *textField;
...
}
RNController.m
- (void)viewDidLoad {
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
}
- (BOOL)textFieldShouldReturn:(id)sender {
NSLog(#"Entering in textFieldShouldReturn ");
[textField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldReturn:(id)sender {
NSLog(#"Entering in textViewShouldReturn ");
[textField resignFirstResponder];
return YES;
}
- (IBAction)textFieldDoneEditing:(id)sender {
NSLog(#"Entering in textFieldDoneEditing ");
[sender resignFirstResponder];
}
- (IBAction)textViewDoneEditing:(id)sender {
NSLog(#"Entering in textViewDoneEditing ");
[sender resignFirstResponder];
}
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
EDIT: The textField is created dinamically like this:
- (void) showPreguntaTexto: (Pregunta *) pregunta {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetWidth(baseView.bounds)*0.1, offset + CGRectGetWidth(baseView.bounds)*0.05, CGRectGetWidth(baseView.bounds) - CGRectGetWidth(baseView.bounds) * 0.2 , CGRectGetWidth(baseView.bounds)*0.5)];
textField.delegate = self;
[vistaAnterior addSubview:textField];
}
My views are the baseView (with elements that do not change) and vistaAnterior, that has the content (and the textField) and changes.
Trying this, it shows that entered to textFieldShouldReturn, but the keyboard does not dissapear.
Why is this happening?? Please help!!
Resign the sender of the textfield instead of your instance. UITextField *textField is not an IBOutlet (storyboard) or created in code so textField is nil (unless you created it somewhere else and didn't show the code).
- (BOOL)textViewShouldReturn:(id)sender {
NSLog(#"Entering in textViewShouldReturn ");
[sender resignFirstResponder];
return YES;
}
have you set text fields delegate in your RNViewController from stroyboard. This might be a reason for keyboard not hiding.
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;
}