resignFirstResponder programmatically [duplicate] - ios

This question already has answers here:
Understanding resignFirstResponder with UITextField
(3 answers)
Closed 9 years ago.
How do I remove a keyboard from the view as a result of the return key being touched when the UITextField was created programmatically.
If the UITextField was called in the viewDidLoad I know how to do this, but the UITextField was created as a result of an -(IBAction).
I created my UITextField programmatically. I know the resignFirstResponder removes the keyboard. I have it set up to do so when the screen is taped outsie the keyboard. I also have it working to where if the user triggers the IBAction with the UIButton related to the UITextField the keyboard goes away. I also want to be able to hide the keyboard when the user selects return from the keyboard.

You need to make yourself a UITextFieldDelegate and implement:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Make sure you set the textField's delegate to self when you create it.

You can use this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
You'll need to set your text field's delegate to self first, though:
self.textField.delegate = self;
Or, you could right-click-drag from the text field in IB to little orange circle at the bottom.

Related

how can i edit the app extension custom keyboard,when use UIButton

I want to edit my customKeyboard when I tap the UIButton that in my custom keyboard.
I have already add a UIButton in the keyboard,and the code like this:
- (IBAction)resignKeyboardButtonDidTouch:(id)sender {
[self.view endEditing:YES]; // it doesn’t work
}
You can resign keyboard for specific textField like,
[yourTextfield resignFirstResponder];
So, try this. You can set same tag to your button and textfield to identify particular textfield.

Tapping between UITextFields in iOS7

When a UITextField is selected and has a keyboard shown, if I tap other parts of the view the keyboard disappears.
If I tap another UITextField, the keyboard stays up, the first textfield is deselected, and nothing happens. Then I need to tap on the other UITextFIeld again for selection to happen and a keyboard to appear.
Is there a way to make a second UITextField immediately accessible when a first UITextField is selected?
If you reload the tableview in textFieldDidEndEditing, you'll break selection in this way. Don't do that.
try it, press another view should call below fn.
-(void)disappearKey{
[self.view endEditing:YES];
}
after keyboard disappear, Tap any textfield, will appear keyboard.
First of all I think its a bug that the keyboard is not dismissed and opened again when tapping on another UITextField or UITextView. It should be reported and Apple should fix it.
Using the textfield delegate methods and registering for keyboard notification it should be possible to manually keep track if the user tapped on another textfield and the keyboard did not close and reopen. At the very least you should be able to detect when this is happening and close the keyboard manually by [textField resignFirstResponder];
The keyboard notification are as follows:
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
I'm''pretty sure you know the UITextfield and textview delegate methods
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
I am not in an active project at the moment so I'm not sure if I just ignored the problem but I can't recall this happening to me.
you can use BSKeyboardControls. just see the demo and decide to
use or not.
or you can do you have to set tag in sequence to the each textfield
in uiview. then use the below code.
-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return NO;
}

dismiss Keyboard not working

Hi, I am not able to dismiss keyboard. I have a scroll view which have UITextFields on a it. I tried using
[self.view endEditing:YES];
And
[self.scrollView endEditing:YES];
I tried using resignFirstResponder on individual textfields but no use.
This issue only occurs when I tap a textfield which I am using as button when taped upon it I use
[textField resignFirstResponder];
but old one don't resign whatever I do like I tried using endEditing before I resigned the button like textfield. So my question is what could be the problem in my scenario and is there any way to forcefully dismiss keyboard?
If you want textField to act as button, use delegates.
textField.delegate=self;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

Bringing up the iPhone keyboard by clicking a button?

How can I bring up the iphone keyboard by clicking a button - I haven't created any text fields. My intention is to have labels that hold a single character.
Other people have asked this and it has been answered but it was from years ago and I didn't understand what people meant in there responses.
Thank you for any help in advance.
Only way you could do this is via hidden UITextField, and set that textField to becomeFirstResponder
you could hide the text field in your code liket his textfield.hidden=YES;
or you could hide the textfield from nib file also by going into the attribute inspector and tick the Hidden property
You could have a look at this UIKeyInput- Displaying keyboard in iPhone without UITextField or UITextView, I have not tried myself this
Attach the button to a touchUpInside event and call becomeFirstResponder on the textView or textfield:
Here is an example (to bring it up when a text view is clicked):
-(IBAction)buttonClicked:(id)sender{
[textView becomeFirstResponder];
}
-(void)ButtonClicked
{
[textFld becomeFirstResponder];
[textView becomeFirstResponder];
}
use this method
You can add a hidden UITextFeild as tempTF and when the button clicked. call becomeFirstResponder of this textFeild:
[self.tempTF becomeFirstResponder];
To Hide textField:
UITextField *tempTF = [[UITextField alloc] init];
tempTF.hidden = YES
or mark Hidden ir your Interface Builder

Resources