I'm trying to hide the keyboard when the user selects a UITextField. I currently have three text fields: two UIPickerView (controlled by the same picker) and a textinput field. When I click "return" or on the background, the keyboard disappears via a resignFirstResponder call.
PROBLEM:
When I am currently editing the text input field and then immediately select the UITextField w/ UIPickerView functionality, the keyboard doesn't disappear. I feel like i've tried every solution and am beating by head against a wall...
CODE:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[pickerView setHidden:YES];
if (fldQuiver.editing == YES) {
[fldTitle resignFirstResponder];
[fldQuiver resignFirstResponder];
[pickerView setHidden:NO];
variabla = 1;
}else if (fldCategory.editing == YES) {
[fldTitle resignFirstResponder];
[fldCategory resignFirstResponder];
[pickerView setHidden:NO];
variabla = 2;
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
}
Any help would be appreciated. Thanks in advance.
EDITED CODE FOR PRINCE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
{
//set logic for picker view here
if (textField == fldQuiver)
{
variabla = 1;
}
else if (textField == fldCategory) {
variabla = 2;
}
else
{
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
if (textField == fldQuiver)
{
[fldTitle resignFirstResponder];
[pickerView setHidden:NO];
return NO;
}
else if (textField == fldCategory) {
[fldTitle resignFirstResponder];
[pickerView setHidden:NO];
return NO;
}
else
{
[pickerView setHidden:YES];
return YES;
}
}
ALSO: I have "synthesized" and declared #property fldQuiver and fldCategory. I have IBOutlet for fldTitle, fldQuiver, fldCategory.
The pickerView is loaded with an array based on variable.
Use textFieldShouldBeginEditing delegate method for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
{
//set logic for picker view here
if (textField == fldQuiver)
{
//picker view hidden or show here
return NO;
}
else if (textField == fldCategory) {
//picker view hidden or show here
return NO;
}
else
{
return YES;
}
}
set delegate for text field
textField.delegate=self;
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
...........
[textField resignFirstResponder]; //it common for all text field,so not use multi resignFirstResponder
............
}
Related
I have many text fields arranged vertically on the iPhone screen. Each field call the keyboard.
The problem is with the fields at the bottom of the screen, because the keyboard pops up and covers them, so the user can not see them.
I've added UIScrollView and drag all the text fields on it - but the scrolling does't work.
Should I add some code?
How do I implement the scroll view?
add a delegate for all the textfields and use the below code as per your requirement
here self.scrMain is my scrollview object
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == txtRegNo) {
[self.scrMain setContentOffset:CGPointMake(0, 20) animated:YES];
}
if (textField == txtName) {
[self.scrMain setContentOffset:CGPointMake(0, 80) animated:YES];
}
if (textField == txtMobile) {
[self.scrMain setContentOffset:CGPointMake(0, 150) animated:YES];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.scrMain setContentOffset:CGPointMake(0, 0) animated:YES];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.scrMain setContentOffset:CGPointMake(0, 0) animated:YES];
[textField resignFirstResponder];
}
I have a UIScrollView in which inside of that I have a UITextField, so what I did is that on the third text field, which is password, I scrolled down the offset of the UIScrollView:
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ((textField == self.emailTextField_ || textField == self.passwordTextField_)){
if (self.scrollView_.contentOffset.y != self.emailTextField_.frameY - self.emailTextField_.frameHeight/2){
shouldAdjustOffset = YES;
} else {
shouldAdjustOffset = NO;
}
if (shouldAdjustOffset){
[self.scrollView_ setContentOffset:CGPointMake(0, self.emailTextField_.frameY - self.emailTextField_.frameHeight/2) animated:YES];
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstNameTexField_){
[self.firstNameTexField_ resignFirstResponder];
[self.lastNameTextField_ becomeFirstResponder];
} else if (textField == self.lastNameTextField_){
[self.lastNameTextField_ resignFirstResponder];
[self.emailTextField_ becomeFirstResponder];
} else if (textField == self.emailTextField_){
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
} else if (textField == self.passwordTextField_){
[self signupButtonPressed:nil];
[self.passwordTextField_ resignFirstResponder];
}
return YES;
}
Now the issue is that when I am on the next text field it resets the content offset back to 0, when I do:
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
. How can I prevent this from happening?
Just turn of scrollview scroll before showing keypad (becomeFirstResponder) and enable back the scroll after it,
scrollView.scrollEnabled = NO;
[someControl becomeFirstResponder];
scrollView.scrollEnabled = YES;
[scrollview doTheScrollingHere];
#Deepan you saved me a lot of time :) Only what I'll suggest is to override UITextField method becomeFirstResponder to check first if it is in UIScrollView and if is disable scroll for a moment
- (BOOL)becomeFirstResponder
{
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:NO];
}
BOOL accept = [super becomeFirstResponder];
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:YES];
}
return accept
}
How do I use the next button (the done button) in the bottom left of the keyboard on the iPhone?
if i understand the question.
Here is solution
Code snippit from link:
- (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;
}
I have two text fields in my view. I did it using IB. My problems are
After entering the text in textField1 I am entering text in textField2. When I click in textField1 again the previous text is disappeared in the textField1.
After entering the text in both the textFields, I need the keyboard to disappear. But, even I touched the return key in the keyboard layout or I touched the screen outside the text field the keyboard is not disappearing.
How can I make this.
Thank you.
- (void)viewDidLoad
{
self.title = #"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
//[nameField becomeFirstResponder];
nameField.clearsOnBeginEditing = NO; //First text field
[nameField resignFirstResponder];
//[descriptionField becomeFirstResponder];
descriptionField.clearsOnBeginEditing = NO; //second text dield
[descriptionField resignFirstResponder];
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[nameField resignFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
I did in the above way but the keyboard is still not disappearing. And after entering the text in textField1 when I press return key the cursor is not going to textField2. How can I make it work ?
Thank You.
For problem 1, check the clearsOnBeginEditing property of the text field.
For problem 2, you can send the resignFirstResponder message to the text field.
See Managing Keyboard section in the UITextField documentation.
I got the result. First thing in my mistake is I did not add UITextFieldDelegate in interface. And the remaining I did the changes in code.
- (void)viewDidLoad {
self.title = #"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
[nameField becomeFirstResponder];
nameField.delegate = self;
descriptionField.delegate = self;
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"Return Key Pressed");
if (textField == nameField) {
[nameField resignFirstResponder];
[descriptionField becomeFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
After entering the text in textField1 if I touch return the cursor goes to textFeild2. It is working good. But when I touch the screen out of text field the keyboard is not disappearing.
Thank you.
I have a login screen with two textfields (username and password). When the user clicks on either textfield, the keyboard appears and everything is fine. However, if the user clicks on the other textfield before clicking Done (on the keyboard) for the first textfield, the text for the username isn't saved. The only way to save the original textfield text is by clicking back on it and selecting Done then.
I would like to disable the other textfield from displaying the keyboard while the first textfield's keyboard is still open, if that makes sense? I know there's ways to stop textfields from being editable but how can I tell when there is already a keyboard open?
Another solution may be to disable the keyboard whenever the user clicks anywhere outside of the textfield? How would I do this?
Here's my code:
textFieldEmail = [[UITextField alloc] initWithFrame:frame];
textFieldEmail.keyboardType = UIKeyboardTypeEmailAddress;
textFieldEmail.returnKeyType = UIReturnKeyDone;
textFieldEmail.tag = 0;
textFieldEmail.delegate = [[UIApplication sharedApplication] delegate];
textFieldPassword = [[UITextField alloc] initWithFrame:frame];
textFieldPassword.keyboardType = UIKeyboardTypeEmailAddress;
textFieldPassword.returnKeyType = UIReturnKeyDone;
textFieldPassword.tag = 1;
textFieldPassword.delegate = [[UIApplication sharedApplication] delegate];
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Thanks!
I you want this only you can do this like..
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == textFieldEmail){
[textFieldPassword setUserInteractionEnabled:NO];
}
else if(textField == textFieldPassword)
[textFieldEmail setUserInteractionEnabled:NO];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textFieldEmail setUserInteractionEnabled:YES];
[textFieldPassword setUserInteractionEnabled:YES];
[textField resignFirstResponder];
return YES;
}
But one thing I want to add, blocking UI is a very bad idea in developing iPhone apps. There are many workarounds to achieve your goal. Try not to use the blocking UI.
Hope this helps.
Thanks
you can make some flag, for example smth like neededInfoHasBeenEntered of BOOL type and return it in your textFieldShouldBeginEditing delegate method for all other textFields
I got the same problem and I tried the above code and it was working fine. This code is very much helpful for me.
I have 4 textFields in the my view and I used the code as:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == eventTextField){
[eventPlaceTextField setUserInteractionEnabled:NO];
[wineryTitleLabel setUserInteractionEnabled:NO];
[vintageTextField setUserInteractionEnabled:NO];
}
else if(textField == eventPlaceTextField)
{
[wineryTitleLabel setUserInteractionEnabled:NO];
[vintageTextField setUserInteractionEnabled:NO];
}
else if(textField == wineryTitleLabel)
{
[vintageTextField setUserInteractionEnabled:NO];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
int vintageVal = [vintageTextField.text intValue];
if(textField == eventTextField)
{
event.eventName = eventTextField.text;
[eventTextField setUserInteractionEnabled:YES];
[eventPlaceTextField becomeFirstResponder];
[textField resignFirstResponder];
}
else if(textField == eventPlaceTextField)
{
event.eventPlace = eventPlaceTextField.text;
[eventPlaceTextField setUserInteractionEnabled:YES];
[wineryTitleLabel becomeFirstResponder];
[textField resignFirstResponder];
}
else if(textField == wineryTitleLabel)
{
event.eventWinery = wineryTitleLabel.text;
[wineryTitleLabel setUserInteractionEnabled:YES];
[vintageTextField becomeFirstResponder];
[textField resignFirstResponder];
}
else if(textField == vintageTextField)
{
if([vintageTextField.text length] == 4 || [vintageTextField.text length]==0)
{
event.eventVintage = vintageVal;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"!!!WARNING!!!" message:#"Enter the Vintage in the format 'YYYY'"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
}
[vintageTextField setUserInteractionEnabled:YES];
[self setViewMovedUp:NO];
[textField resignFirstResponder];
}
return YES;
}
You can approach this on two different ways :
Disable the userInteraction of the textfield on
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textfield == textFieldEmail)
{
textFieldPassword.userInteractionEnabled = NO;
}else if(textfield == textFieldPassword){
textFieldEmail.userInteractionEnabled = NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
textFieldPassword.userInteractionEnabled = YES;
textFieldEmail.userInteractionEnabled = YES;
}
OR
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UITextField *theOtherTextField;
if(textView == textFieldEmail)
{
theOtherTextField = textFieldPassword;
}
else if(textfield == textFieldPassword)
{
theOtherTextField = textFieldEmail;
}
return ![theOtherTextField isFirstResponder];
//Return no if the other text field is the first responder
}