How can I make the keyboard disappear after entering the text? - iphone

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.

Related

Keyboard not resigning second time?

Keyboard not resigning second time on the same textfield. I used UITextFieldDelegate. For Ex: I enter something in Name textField and I resign the keyboard. I clicked in MobileNo field entered something. This time keyboard not resigning.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Make delegate of your MobileNo
MobileNo.delegate=self;
Or if your are using xib then make Outlet connection of delegate
Try this,
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}
-(void)dismissKeyboard
{
[self.view endEditing:YES];
}

Hide keyboard when selecting another UITextField w/ UIPickerView error

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
............
}

UITextField missing the paste functionality

I can provide code if needed, however my problem looks fundamendal. I have a UITextField in a view that can copy and paste in it. After the action I cannot do it again. It works only once.
What might be the reason behind it? Is it possible that the paste menu is not shown because of another view in the window?
some code:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,1,320,50)];
[myTextField setFont:[UIFont boldSystemFontOfSize:40]];
[myTextField setTextColor:[UIColor whiteColor]];
[myTextField setText:#""];
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField setEnabled:YES];
[myTextField setKeyboardType:UIKeyboardTypePhonePad];
[myTextField setDelegate:self];
myTextField.inputView = hiddenView;
and
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action == #selector(paste:))
return YES;
return NO;
}
Do I need to add something in the viewWillAppear method related with the UITextField? As I said the first time works fine.
UPDATE: After the first paste the copy/paste/select mechanism stopped working on my application in ALL views...
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (sel_isEqual(action, #selector(paste:)))
{
return YES;
}
return [super canPerformAction:action withSender:sender];
}

ResignFirstResponder doesn't dismiss the keyboard (iPhone)

I've searched through this site that I couldn't find a solution to the problem I'm facing now. Hope someone can help.
I've created a UIAlertView to prompt user to enter their name in an iPhone app.
UIAlertView *enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameField becomeFirstResponder];
[enterNameAlert addSubview:enterNameField];
[enterNameAlert show];
[enterNameAlert release];
[enterNameField release];
I've setup this viewController to comply with UITextFieldDelegate in the header file, and implemented textFieldShouldReturn: trying to dismiss the keyboard when user hit Done.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
In the debugger, I confirm that this method is called successfully when I tap on the Done button, with the textField being the first responder at that time. However, the keyboard doesn't go away, but the little clearButton goes away. It is clear that the textField is no longer the first responder because when I click the Done button again, nothing get called. I just want to dismiss the keyboard with the Done button. Can anyone please offer a solution? Million thanks.
First you need to define your enterNameAlert in interface header. then use the below code.
- (void)viewDidLoad {
[super viewDidLoad];
enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameAlert addSubview:enterNameField];
[enterNameField becomeFirstResponder];
[enterNameAlert show];
[enterNameField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
[enterNameAlert dismissWithClickedButtonIndex:1 animated:YES];
[enterNameAlert release];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
What happens if you move the releasing of the text field to the delegate method, after you call the resignFirstResponder method?
Try dismissing keyboard using resignFirstResponder on "DidEndOnExit" event of your TextField.
Then try to dismiss keyboard by pressing Done button on keyboard when you have finished entering the data into your TextField.
Hope this helps.
Have another object become and then immediately resign the first responder.
simply do
[mytextField addTarget:self
action:#selector(methodToFire:)
forControlEvents:UIControlEventEditingDidEndOnExit];
if anyone is having trouble with this in storyboard i had to go into my uitextfields properties and ctrl+drag from its delegate to its root view controller all in storyboard and that fixed everything..

iPhone textfields/keyboard - how to disable second textfield until first one has been completed

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
}