Hopefully somebody can help me out here. I have a UIView with 3 text fields. I want the "return" key from the keyboard to resign the first responder, regardless of which text field the users curser is in. My code works for 2 of the 3 text fields, but consistently (as in always) it refuses to resign first responder from the 3rd text field, and I don't know why (the text fields are identical. The field that does not work is the 3rd field...
Here is the code if it helps
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[firstTextField resignFirstResponder];
[secondTextField resignFirstResponder];
[thirdTextField resignFirstResponder];
return YES;
}
Here is the property declaration in the H
#property (weak, nonatomic) IBOutlet UITextField *firstTimeTextField;
#property (weak, nonatomic) IBOutlet UITextField *secondTextField;
#property (weak, nonatomic) IBOutlet UITextField *thirdTextField;
It seems pretty straight forward, so I'm wondering if there is some kind of error checking that I should be doing to determine whats happening here.
The file is declared as a UITextFieldDelegate as well...
Thanks for the time and the assistance!
Have you set the delegate to ALL UITextFields?
Set the delegate to all UITextFields.
Related
I have four UITextFields and associated each with an outlet in .h files. I have also defined 4 dismiss function for each textfield as shown below:
- (IBAction)dismiss1:(id)sender;
- (IBAction)dismiss2:(id)sender;
- (IBAction)dismiss3:(id)sender;
- (IBAction)dismiss4:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *name;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *weight;
#property (strong, nonatomic) IBOutlet UITextField *age;
Implementation of dismiss function in the .m file:
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss2:(id)sender {
[sender resignFirstResponder];
}
...
I am very sure that the outlet is connected to each UITextFiled correctly. The IBAction of each dismiss function is also connected with 'Editing did end' event correspondingly. However, when I ran the app using simulator, the keyboard will not dismiss when I click 'Enter/Done'. It's also very weird that when I place the breakpoint inside the dismiss function, clicking 'Enter' when typing in the corresponding UITextField does not bring up the debugger.
Thanks a lot for helping!
Update: I checked the object type of sender (dismiss1) using breakpoint and it's UITextFiled. However, I did not entered the debugging mode when I click 'Enter' in the first TextField, but entered the debugging mode when I click on the second TextField (before typing).
set UITextFielDelegate
textfieldname.delegate = self;
use this code it will resolve your issue
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Use the code to resign keyBoard without specify textFiled name.
- (IBAction)dismiss1:(UITextField*)sender {
[self.view endEditing:true];
// or use [sender resignFirstResponder];
}
Add text field delegate
name.delegate = self;
email.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
No need to create four actions for textfield
You can set delegate of Textfield and use following method
- (void)textFieldDidEndEditing:(UITextField *)textField
Inside this method you can compare your textfield with method returning textfield and get you task done in "if" condition
Hope it help you!!
Assign the tag to different textfields
then >
textfield.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textfield.tag==num)
{
[textField resignFirstResponder];
}
return YES;
}
The Editing did end is called when a textfield did lose the "focus", mean it is not the first responder any more (because it has resigned or an other one became first responder).
Use textFieldShouldReturn to detect when the return key is called.
And call resignFirstResponder on the texfield when this is called. The Editing did end event will occur after that.
Edit : you have to implement something (Your ViewController?) as delegate of the textfield.
I am stuck in a very strange problem. A default text field in iOS 8 & 7 behave very strange.
If the text box got focused the alignment of text get changed check the screen shots.
even with very small text size
Edit:
#import <UIKit/UIKit.h>
#interface LoginForm : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *txt_username;
#property (weak, nonatomic) IBOutlet UITextField *txt_password;
- (IBAction)loginButtonPressed:(id)sender;
#end
Try this. Its more of a work around, not exactly getting to the root of the problem but make your VC a UITextFieldDelegate, then make it the textFields delegate and use the following code. -
(void)textFieldDidBeginEditing:(UITextField *)textField
{
// Change the alignment if you need to.
textField.textAlignment = NSTextAlignmentCenter;
}
It should be a alignment issue. In your storyboard check the UITextField Alignment matches with the below image.
In some sense this question has already been answered at Limit number of characters in uitextview. But my particular case is that I have more than one textview in the same ViewController. So I am not sure how to fix that problem. Say I only have two textViews. How might I handle these cases:
they both have the same character limit?
each has different character limit? say 300 and 400 respectively.
Do I use IBAction? If yes how?
So you need IBOutlet for both textviews
#property (weak, nonatomic) IBOutlet UITextField *textfield1;
#property (weak, nonatomic) IBOutlet UITextField *textfield2;
then in your delegate method
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
You simply add a check for the right textfield
if (self.textfield1 == textfield) {
// handle first text field here
} else {
// handle second text field here
}
I have few text fields in a UIViewController. For some of the text fields I have used IBOutlet to make property. For some textFields that I add programmatically (since they are in scroll view, they are not in the constraints of the view controller window in soryboard. ) , I have just made them property without IBOutlet.
So for example, I have:
#property (weak, nonatomic) **IBOutlet** UITextField *descriptionTextBox;
#property(strong , nonatomic) UITextField *cityTextField;
Now I set the delegates of both in ViewDidLoad & also in .h file <UITextFieldDelegate>
But after implementing - (BOOL)textFieldShouldReturn:(UITextField *)textField method , the keyboard only return for the text field having IBOutlet.What can I do for it?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; //works only for text field declared in storyBoard.
return YES;
}
In your view controller, say [self.view endEditing:YES]. This will dismiss the keyboard regardless of who is first responder.
I think it would be helpful to see more code.
However your issue could be that you're not setting the delegate on your UITextFields hence the method only being called on the one in the storyboard.
Check to see whether you have set delegate to your UITextField objects that were created programatically.
I'm making an application that requires the use of multiple textFields with number pads as there first responder. I have created an image to use as a negative button that will be an addition to the number pad.
I am wondering if there is a way to check which textField the number pad is typing to.
Any help would be appreciated!
If all of your potential first responders are UITextFields, another approach would be to conform your controller class to UITextFieldDelegate protocol, and then grab a reference to the currently editing UITextView at the time it begins editing.
Conform your class in your .h:
MyController : NSObject <UITextFieldDelegate> //Might often be a UIViewController rather than an NSObject subclass...
Define a property:
#property (weak, nonatomic) UITextField *editingField;
Then synthesize in your .m:
#synthesize editingField = __editingField
Then implement:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setEditingField:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setEditingField:nil];
}
Now, whenever you want to know which text field is your first responder:
UITextField *firstResponder = [self editingField];
If there are only a limited number of them, you can query each with [textFieldN isFirstResponder]. If you want a general purpose utility, you can look at each subview in a view and see whether it is the first responder or whether any of its subviews farther down the hierarchy are.