I have two UITextFields that I want to use for logging in, one for the username and one for the password. Before I wasn't able to dismiss the keyboard in the first UITextField but I managed to fix that, the problem now is that once it dismisses I cannot click on the other UITextField to enter in the password, I am just stuck in the username UITextField with the keyboard dismissed, so especially I can't do anything. Any suggestions on how to dismiss the keyboard and then click on the other UITextField to enter the information?
#interface LoginViewController () <UITextFieldDelegate>
#end
#implementation LoginViewController
#synthesize managedObjectContext, usernameField, passwordField;
-(void) textFieldDidEndEditing:(UITextField *)textField{
}
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[self.usernameField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//error occurs here
[self.usernameField resignFirstResponder];
return YES;
}
Just do this, this thing will be simple
Set the tag for the textfields
usernameField.tag=1;
passwordField.tag=2;
Then a delegate method to dismiss keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[userNameField resignFirstResponder];
[passwordField resignResponder];
}
return YES;
}
Update me the result of this
You have problem with your code at textFieldDidBeginEditing
Everytime you are opening keyboard for username field while begin editing.
-(void) textFieldDidEndEditing:(UITextField *)textField{
if(textField==self.usernameField)
{
[self.passwordfield becomeFirstResponder];
}
}
-(void) textFieldDidBeginEditing:(UITextField *)textField{
// [self.usernameField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//error occurs here
if(textField==self.passwordField)
{
[self.view EndEditing:YES];
}
return YES;
}
The easiest solution to the above problem is changing the textFieldDidBeginEditing function to:
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField becomeFirstResponder];
}
And changing the textFieldShouldReturn to the following
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
The above code allows the user to choose the order of input. That is, the user can input the password first, and then the username. If you want the user to input the username first at all times, you could change the textFieldDidBeginEditing to
-(void) textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.passwordField && [self.userField.text isEqualToString:#""])
[self.usernameField becomeFirstResponder];
else
[textField becomeFirstResponder];
}
This will ensure that the username field is always selected if it is empty. If it is not, then the program will select whichever textfield the user touched, for editing.
I hope this answers your question, though a little late. I think it is an easier way than using and monitoring tags.
Related
I have one view in my app where there is 1 text field. And I've noticed that keyboard appears after second tap.
But it's interesting that on iPhone it's time to time (some time appears after first tap at once, and some time after second tap only).
On iPad looks like it more ofter appears after second tap only.
I use UITextFieldDelegate
in viewDidLoad I assign the delegate _locationTextField.delegate = self;
and I use delegate methods textFieldDidBeginEditing, textFieldDidEndEditing, textFieldShouldReturn
e.g.:
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_locationNameBeforeManualEdit = _locationTextField.text;
// save the previod city value to compare after did end editing
NSLog(#"textFieldDidBeginEditing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self continueButtonPressed:nil];
// [textField resignFirstResponder];
return YES;
}
in storyboard
What could be the problem?
Found solution here on Stackoverflow - solution related with keyboard preload:
- (void)preloadKeyboard {
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
}
This method should be used in application: didFinishLaunchingWithOptions: method of AppDelegate.
I have a custom UIView which creates a UITextField as a subview while the application is running. I have been stuck on this for hours now, but cannot figure out how to create delegates to hide the keyboard and determine when the "done" button is pressed. I'm still pretty new to iOS development, so any help here would be greatly appreciated.
Thanks.
u can do this set
yourtextfieldname.delegate=self;
when u hide your keyboard.. then u can call that textfield and do this code...
[txtfieldname resignFirstResponder];
or u can try this.. also
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfieldname resignFirstResponder];
}
or best method
-(BOOL)textfieldshouldreturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
If you are using keyboard done button, set the delegate to the textfield
texfield.delegate = self;
then the below method will be called when done button is clicked
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you are using done button on a toolbar created by you then add a target to the doneButton then call below method in the selector
[textField resignFirstResponder];
This may help you.First use UITextFieldDelegate and set
yourtextfield.delegate=self;
Then use
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
}
// and also you can use this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
return YES;
}
Hello i am making an app that uses a login page and registration page. I need to make it so when in the login page for example, i can press the return key (customised to say next) and it will move the cursor to the next text field. I have researched it a lot and have found some code that should work however no matter what i do it won't work.
Code 1 -
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == field1TextField) {
[textField resignFirstResponder];
[field2TextField becomeFirstResponder];
}
else if (textField == field2TextField) {
[textField resignFirstResponder];
[field3TextField becomeFirstResponder];
}
else if (textField == field3TextField) {
[textField resignFirstResponder];
}
return YES;
}
This makes sense to me however it wont work.
My first text field is called txtUsername and my second text field is called txtPassword. So i customized the code like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == txtUsername) {
[textField resignFirstResponder];
[txtPassword becomeFirstResponder];
}
return YES;
}
I have setup a custom .h & .m file and am placing the code in there. Should i be linking the code to the text field somehow?
In order to call the textFieldShouldReturn method you will need to set the delegate of the UITextField. In your viewDidLoad method, add:
txtUsername.delegate = self;
txtPassword.delegate = self;
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;
}
In my app i'm using a UITextField collect a string value. Whenever i finish editing the field, textFieldDidEndEditing gets fired, but later textFieldShouldReturn never. What can be it's reason?
I provide some code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField == self.urlPatternTextField) {
/*do some stuff*/
}
}
- (IBAction)dismissKeyboard:(id)sender {
NSMutableArray* possibleReponders = [[NSMutableArray alloc]initWithObjects:
self.urlPatternTextField, nil];
for (UITextView* tv in possibleReponders) {
if([tv isFirstResponder]) {
[tv resignFirstResponder];
return;
}
}
}
Note:
In textFieldDidEndEditing the if condition is true.
Thanks for the help in advance.
Sincerely,
Zoli
textFieldShouldReturn:
Asks the delegate if the text field should
process the pressing of the return button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Parameters
textField The text field whose return button was pressed.
Return Value
YES if the text field should implement its default behavior for the
return button; otherwise, NO.
Discussion The text field calls this method whenever the user taps the
return button. You can use this method to implement any custom
behavior when the button is tapped.
The method invokes when return button is pressed.Try it so