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];
}
Related
In our app, there's a situation where the user enters something in a textbox and then presses the back button to go back to the master screen.
If we run that on iOS 7, the keyboard does not disappear, it just stays there. The user can still navigate through the app, but all text fields are disabled, meaning you can't enter text anywhere. The only option the user has is killing the app and starting fresh.
We tried to add resignFirstResponder messages, but that didn't help anything.
There's much code involved, and we're actively working on the issue. Meantime, has anyone experienced that problem too, and maybe found a way to make it go away?
I had the same issue like you when I compiled the app for iOS 7 and I did the following changes:
Make sure you add [textfield resignFirstResponder] before dismissing the viewController for example:
[_passwordInput resignFirstResponder];
[_emailInput resignFirstResponder];
[self performSegueWithIdentifier:#"forgotPassword" sender:self];
Just to be sure the keyboard disappears add [textfield resignFirstResponder] in viewWillDisappear for example :
- (void) viewWillDisappear:(BOOL)animated
{
[_passwordInput resignFirstResponder];
[_emailInput resignFirstResponder];
}
If your viewController is presented using UIModalPresentationFormSheet add this to your viewController just to make sure the textfields will respond resignFirstResponder:
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
In your case, override the back button action or just use viewWillDisappear to check when the user pressed the back button and then call resignFirstResponder before [super viewWillDisappear] something like this:
-(void) viewWillDisappear:(BOOL)animated
{
[_passwordInput resignFirstResponder];
[_emailInput resignFirstResponder];
[super viewWillDisappear:animated];
}
Try [self.view resignFirstResponder], instead of [textfield resignFirstResponder] on viewWillDisappear.
[textfield resignFirstResponder] should do the job, but to make sure and for not to loop through all your textFields you can use:
[self.view endEditing:YES];
From the doc:
use to make the view or any subview that is the first responder resign
(optionally force).
in general I find this useful
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
you can add this in viewWillDisappear: or viewDidDisappear:
this will hide the keyboard without a reference to the currently focused text field
I had the same problem only with MoreViewController in UITabBarController (iOS 8.3). Maybe this solution not very 'nice' and little complicated, but seems like it works, hope it will help you too.
#interface ViewController ()
#property (nonatomic) BOOL needToHideKeyboard;
#property (nonatomic, strong) IBOutlet UITextField *txtField;
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.needToHideKeyboard = NO;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self hideKeayboard];
}
- (void)hideKeayboard
{
if (self.needToHideKeyboard) {
[self.txtField resignFirstResponder];
}
}
- (void)keyboardWasShown:(NSNotification *)notification
{
self.needToHideKeyboard = YES;
NSDictionary *info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Shift scroll view content insets on the keyboard height
UIEdgeInsets contentInsets = self.scrollView.contentInset;
contentInsets.bottom = keyboardSize.height;
self.scrollView.contentInset = contentInsets;
}
- (void)keyboardWillBeHidden:(NSNotification *)notification
{
self.needToHideKeyboard = NO;
// Reset keyboard content insets
UIEdgeInsets contentInsets = self.scrollView.contentInset;
contentInsets.bottom = [self.bottomLayoutGuide length];
self.scrollView.contentInset = contentInsets;
}
#end
If your view controller implements textFieldDidEndEditing, make sure you don't set another view to be the first responder if the view is disappearing. textFieldDidEndEditing will get invoked when you call resignFirstResponder, or [self.view endEditing:YES].
[self.view endEditing:YES];
stopped working on my device iOS9.x
We can also do this in the viewWillDisappear method
for (UIView *subview in self.view.subviews) {
if ([subview canPerformAction:#selector(endEditing:) withSender:nil]) {
[subview endEditing:YES];
}
if ([subview canResignFirstResponder]) {
[subview resignFirstResponder];
}
}
This will loop through the responders and resign the responder status.
I want to implement a function that once I touch the table view, the search bar on the top of the view will resignFirstResponder and the keyboard will retrieve from the view. I relate the follow code to the tableView in the xib file but it seems not working.
- (IBAction)backgroundTap:(id)sender
{
NSLog(#"test : did touch down");
[_searchBar resignFirstResponder];
}
I try another way of implementation by adding the following code in the .m file, but still not working.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"test : did touch down");
UITouch *touch = [touches anyObject];
UIView *view = (UIView *)[touch view];
if (view == self.view) {
[_searchBar resignFirstResponder];
}
}
Hope that someone could help.
Just do..
[Self.view endEditing:YES];
Try doing exactly what you are doing, but call resignFirstResponder twice. I found that I must do that as of iOS 7.
Okay I tested the following with the tabbed application template on Xcode 4.5/iOS 6.
Created a tabbed application.
Created a UIButton subclass called SampleButton and implemented
the following mothods:
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches
withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
}
- (void) touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
}
Added this SampleButton to the first tab.
Added breakpoints to all the touch methods.
Run on device.
Tested that all the touch methods are firing as expected.
Now touch the SampleButton and then also press the second tab.
RESULT: View switches to second tab but touchesCancelled and/or touchesEnded are never called in SampleButton. Shouldn't one or the other of those fire if the view changes while I'm touching that button? This is proving to be a huge issue because, in my app I'm playing a sound while that button is down and it never stops playing if the user switches tabs while pressing it. Seems like this used to work fine in iOS3 and iOS4.
It appears that when a view is removed from its window, it dissociates itself from any touches that were associated with it. So when the touch finally ends, the system doesn't send touchesEnded:… or touchesCancelled:… to the view.
Workaround by disabling tab switching
If you want to just disable tab switching while the button is pressed, you can do that by giving the tab bar controller a delegate and having the delegate return NO from tabBarController:shouldSelectViewController:. For example, in the your test app, you can have FirstViewController make itself the tab bar controller's delegate:
- (void)viewWillAppear:(BOOL)animated {
self.tabBarController.delegate = self;
}
And the view controller can allow the tab bar controller to select a tab only when the button is not pressed (highlighted):
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
return !_button.highlighted;
}
Workaround by detecting button highlight resetting to NO
When the button is removed from its window, it resets its highlighted property to NO. So one generic way to work around this problem is by using key-value observing (KVO) to monitor the button's state (instead of relying on the button to send you actions). Set yourself up as an observer of the button's highlighted property like this:
static int kObserveButtonHighlightContext;
- (void)viewDidLoad {
[super viewDidLoad];
[_button addObserver:self forKeyPath:#"highlighted"
options:NSKeyValueObservingOptionOld
context:&kObserveButtonHighlightContext];
}
- (void)dealloc {
[_button removeObserver:self forKeyPath:#"highlighted"
context:&kObserveButtonHighlightContext];
}
I discovered in testing that the button sends an extra KVO notification when it's removed from the window, before it resets its highlighted property back to NO. So when handling the KVO notification, check that the value has actually changed:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &kObserveButtonHighlightContext) {
if ([change[NSKeyValueChangeOldKey] boolValue] != _button.highlighted) {
[self updatePlaybackForButtonState];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Finally, start or stop playback according to the highlighted property of the button:
- (void)updatePlaybackForButtonState {
if (_button.highlighted) {
NSLog(#"start playback");
} else {
NSLog(#"end playback");
}
}
Subclassing the button seems like the hard way to do this. Just tell the audio player to stop playing in the viewDidDisappear:animated: method, as R.A. suggested.
I worked around this by setting targets for the actions instead of using the touch methods:
[self addTarget:self action:#selector(handleKeyPressed) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:#selector(handleKeyReleased) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:#selector(handleKeyReleased) forControlEvents:UIControlEventTouchCancel];
These seems to get fired correctly even when the view swaps out.
This question already has answers here:
Dismiss keyboard on touch anywhere outside UITextField
(8 answers)
Closed 9 years ago.
I have a few text inputs and I can hide the keyboard whenever I touch the background, but only when I have been entering into the first text box name textField1. now this code should be simple but I just can't seem to get it.
-(IBAction)backgroundTouched:(id)sender {
[textField1 resignFirstResponder];
[buildLength resignFirstResponder];
[buildWidth resignFirstResponder];
[ridgeWidth resignFirstResponder];
[rafterWidth resignFirstResponder];
[hipWidth resignFirstResponder];
[eaveOverhang resignFirstResponder];
[spacing resignFirstResponder];
}
If you want to hide the keyboard when you tap a button and you have more than one UITextFields in your view, then you should use:
[self.view endEditing:YES];
Tap anywhere on the view, and the keyboard will disappear.
Try this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self view] endEditing:YES];
}
You can also iterate through an array of views (such as your UIView's subviews) and manually resign the keyboard, this is good if you dont want to resign on ALL the subviews within your parent UIView.
- (void)viewDidLoad
{
self.view.userInteractionEnabled = TRUE;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//Iterate through your subviews, or some other custom array of views
for (UIView *view in self.view.subviews)
[view resignFirstResponder];
}
You can try UITouch method, and in this set your text field object and call resignFirstResponder
when ever you touch on the screen the keyboard will resign, I hope this will work for you.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[currentSelectedTextField resignFirstResponder];
}
I've already know how to catch the shake gesture with the code below
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if ( event.subtype == UIEventSubtypeMotionShake )
{
// Put in code here to handle shake
NSLog(#"Device Shaked");
}
if ( [super respondsToSelector:#selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
}
The NSLog shows that it did received the shake
but how to push another view,or back to the last view, as we know the code below can only use in ViewController class instead of View class, this Leavesview is handle by a LeavesviewController, and I use this viewController in another PDFViewController to display, so how could I make it jump to another view or dismiss back???
UIViewController *myView = [[UIViewController alloc]init];
[self.navigationController pushViewController:myView animated:YES];
See this complet Code......
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake)
{
// Work which you want
}
}
also dont forget these two methods...
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self resignFirstResponder];
}
Ok, Obviously I am just a starter 2 months ago. Also, obviously the answer is the Delegation;