I have a UITextField on which I'm calling the animation code below. It runs fine but this code runs when I press the return key. I want this code to automatically run after the user enters any data in the text field without pressing the return key.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[patientNameTextField resignFirstResponder];
[addressTextField resignFirstResponder];
[doctortTextField resignFirstResponder];
[self showAnimationBack];
return YES;
}
After entering any data means you can use this method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//do something here
return YES;
}
or use these methods.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
Use UITextField's Delegate Method :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
This method will be called after every character you input in textfield.
return YES;
for implementing the change you do in this method.
Else
return NO;
Try these delegate methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (void)textFieldDidEndEditing:(UITextField *)textField;
Use
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
[patientNameTextField resignFirstResponder];
[addressTextField resignFirstResponder];
[doctortTextField resignFirstResponder];
[self showAnimationBack];
}
Related
I have set this UIViewController to be the delegate for the UITextField in the viewDidLoad with this line: self.nameInputTextField.delegate = self;.
I have set the delegate on the class as well by adding <UITextFieldDelegate> to the #interface declaration.
When I select the nextButton, in the method that is called, I have tried [self.nameInputTextField resignFirstResponder] as well as [self.view endEditing:YES] one line before I push the new view controller.
The rest of the class does not manipulate the firstResponder.
I've also implemented the UITextField delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.nameInputTextField resignFirstResponder];
return NO;
}
I haven't found any related questions after extensive searching. There are many similar ones about resigning keyboards, but not regarding the timing of the keyboard resignation being postponed until after the view transition is complete. Note- if you reload this url in your browser, you'll see the gif again from the beginning.
Hide keyboard anywhere in ios :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Resign your keyboard on viewWillDisappear and the problem should be solved.
Edit
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.nameInputTextField.text = #"";
[self.nameInputTextField resignFirstResponder];
}
I am reloading my table with reloaddata on rotation of device in my app. If a UITextfield is focused before rotation and keyboard is open , I want it to stay focused and keyboard remains open. Reloaddata calls "textfieldshouldEndEditing" and "KeyboardshouldHide" notifications and thus on rotation my textfield is not focused and keyboard is also closed.
To achieve this I am using the following code:
[myTextfield performSelector:#selector(becomeFirstResponder)
withObject:nil
afterDelay:1.0f];
This works fine and keyboard opens again after rotating the device but the problem is when I press keyboard hide button (now keyboard is closed) and then rotate the device, Keyboard is still showing up which is wrong as before rotating the device I closed it.
Can someone suggest what is wrong or what should be done to achieve this? Thanks
How about just using a BOOL class variable like this:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
hasKeyboardOpen = YES;
}
// your own method to hide keyboard you would call when user physically hide keyboard
- (void)dismissKeyboard
{
[myTextField resignFirstResponder];
hasKeyboardOpen = NO;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(hasKeybaordOpen == YES)
{
[myTextfield performSelector:#selector(becomeFirstResponder)
withObject:nil
afterDelay:1.0f];
}
}
So you before the rotation if the keyboard is visible you it and schedule it to present in 1 second.
I guess the problem is that you schedule the keyboard to present even if it is hidden. If that is the case you should check if the keyboard is visible before performSelector: withObject:afterDelay:
if ([myTextField isFirstResponder]) {
[myTextField resignFirstResponder];
[myTextField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:1.];
}
Although I really don't think performSelector: withObject:afterDelay: is the proper way of doing it. You might want to use willRotateToInterfaceOrientation:duration: and didRotateFromInterfaceOrientation: methods instead. You can store the reference to the textField or any other firstResponder (i.e. if you have multiple textFields or textViews:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([self.myTextFiled isFirstResponder]) {
self.toBeFirstResponderAfterInterfaceOrientationChange = self.myTextFiled;
[self.myTextFiled resignFirstResponder];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (self.toBeFirstResponderAfterInterfaceOrientationChange) {
[self.toBeFirstResponderAfterInterfaceOrientationChange becomeFirstResponder];
self.toBeFirstResponderAfterInterfaceOrientationChange = nil;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.toBeFirstResponderAfterInterfaceOrientationChange = nil;
}
I have an app which involves using different kinds of Gestures. So, in order to differentiate between thse gestures, I used shouldRecognizeSimultaneouslyWithGestureRecognizer method which returns a YES. However, on doing this the keyboard on the UIWebView is dismissed right after it is being presented. And if I don't then the keyboard functions correctly but the controller fails to recognize different gestures.
How do I make the keyboard function correctly and at the same time recognize different gestures ?
Try this one
#pragma mark -
#pragma mark UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
//You can use like this in your shouldRecognizeSimultaneouslyWithGestureRecognizer method
[textView resignFirstResponder];
I have two UITextViews in one UI, One of the UITextView is the first responder. The other UITextView is non-editable. When I double tap the non-editable UITextView, the keyboard disappears and I want to avoid this. They keyboard should always stay.
If you double tap on a text view, it shows UIMenuController with Cut, Copy, etc options.
So to achieve your requirement, set User Interaction property to NO (False).
Hope this is what you are looking for.
-Mrunal
Make your viewController the delegate of the textView and return NO from the UITextViewDelegate method textViewShouldEndEditing:
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
if (textView == self.editableTextView) {
return NO;
}
return YES;
}
this is not default behavior. to achieve your requirement try this using delagate method shouldChangeTextInRange and make your textview editable
- (void)viewDidLoad
{
[super viewDidLoad];
//nonEditingTextView.editable = NO;
//make the nonEditingTextView editable
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ( textView == nonEditingTextView ) {
return NO;
}
return YES;
}
//Firstly add the below code for keyboard notification into your viewdidload method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keybordWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keybordWillShow:) name:UIKeyboardWillShowNotification object:nil];
//Let the two textview's names be, NonEditable and Editable
//declare a flag globally
bool Appearflag;
//Then implement the two methods as follows
-(void)keybordWillHide:(NSNotification *)notification
{
if ([NonEditable isFirstResponder] && Appearflag)
{
[Editable becomeFirstResponder];
}else if ([Editable isFirstResponder])
{
Appearflag = NO;
}
}
-(void)keybordWillShow:(NSNotification *)notification
{
if ([Editable isFirstResponder])
{
Appearflag = YES;
}
}
How to hide the keyboard programmatically in iphone?
Tell the UIResponder subclass that is currently first responder to resign its first responder status:
[responder resignFirstResponder];
[textFieldName resignFirstResponder];
It's easy:
ObjC
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Swift
UIApplication.shared.keyWindow?.endEditing(true)
take a look at UIView Class Reference for endEditing.
Causes the view (or one of its embedded text fields) to resign the first responder status. And keyWindow is the only window that receives keyboard events, so this solution is guarantied to always work.
Call this in your ViewController
[self.view endEditing:YES];
If you are using textview then
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
[self keyboardWillHide];
}
}
and if you are using textfield then
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
}
Here is the swift version:
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)