10

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?

4

7 回答 7

8

当我为 iOS 7 编译应用程序时,我遇到了和你一样的问题,我做了以下更改:

  1. 确保[textfield resignFirstResponder]在关闭 viewController 之前添加,例如:

    [_passwordInput resignFirstResponder];
    [_emailInput resignFirstResponder];
    [self performSegueWithIdentifier:@"forgotPassword" sender:self];
    
  2. 只是为了确保键盘消失[textfield resignFirstResponder]viewWillDisappear例如添加:

    - (void) viewWillDisappear:(BOOL)animated
    {
       [_passwordInput resignFirstResponder];
       [_emailInput resignFirstResponder];
    }
    
  3. 如果您的 viewController 使用UIModalPresentationFormSheet添加到您的 viewController 来显示,以确保文本字段将响应resignFirstResponder

    - (BOOL)disablesAutomaticKeyboardDismissal
    {
       return NO;
    }
    

在您的情况下,覆盖后退按钮操作或仅用于viewWillDisappear检查用户何时按下后退按钮,然后在这样的事情resignFirstResponder之前调用:[super viewWillDisappear]

-(void) viewWillDisappear:(BOOL)animated 
{
   [_passwordInput resignFirstResponder];
   [_emailInput resignFirstResponder];
   [super viewWillDisappear:animated];
}
于 2013-09-19T12:25:25.773 回答
0

尝试[self.view resignFirstResponder],而不是[textfield resignFirstResponder]在 viewWillDisappear 上。

于 2013-09-19T11:12:11.780 回答
0

我只MoreViewControllerUITabBarController( iOS 8.3) 中遇到了同样的问题。也许这个解决方案不是很“好”而且有点复杂,但似乎它有效,希望它也能帮助你。

@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
于 2015-04-17T08:58:14.107 回答
0

[textfield resignFirstResponder] 应该可以完成这项工作,但要确保并且不要循环遍历您可以使用的所有文本字段:

[self.view endEditing:YES];

从文档:

用于使作为第一响应者辞职的视图或任何子视图(可选强制)。

于 2013-09-19T11:22:10.587 回答
0

一般来说,我觉得这很有用

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

你可以添加这个viewWillDisappear:viewDidDisappear:

这将隐藏键盘而不引用当前聚焦的文本字段

于 2015-03-16T13:37:54.883 回答
0
[self.view endEditing:YES];

停止在我的设备 iOS9.x 上工作

我们也可以在viewWillDisappear方法中做到这一点

for (UIView *subview in self.view.subviews) {
        if ([subview canPerformAction:@selector(endEditing:) withSender:nil]) {
            [subview endEditing:YES];
        }
        if ([subview canResignFirstResponder]) {
            [subview resignFirstResponder];
        }
    }

这将遍历响应者并退出响应者状态。

于 2016-02-10T11:32:31.200 回答
0

如果您的视图控制器实现textFieldDidEndEditing了 ,请确保在视图消失时不要将另一个视图设置为第一响应者。 textFieldDidEndEditing当您调用resignFirstResponder, 或时将被调用[self.view endEditing:YES]

于 2015-08-31T22:59:36.950 回答