iOS 7: Keyboard not showing after leaving modal ViewController

戏子无情 提交于 2020-01-25 11:51:23

问题


I've got a HomeViewController that has different modal segues to several other UIViewControllers. If I try to show the keyboard on a UITextField within the HomeView, everything works fine. However, if I try to show the keyboard on a UITextField (using becomeFirstResponder) after returning from any of the modal View Controllers, the keyboard never shows.

Here's some sample code from one of the setups I've tried:

In HomeViewController:

- (void)viewDidAppear:(BOOL)animated
{
    static BOOL firstTimeComplete = false;
    if (!firstTimeComplete) {
        firstTimeComplete = true;
    } else {
        UITextField *textField = [[UITextField alloc] init];
        [self.view addSubview:textField];
        [textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:3]
    }
}

In ModalViewController:

- (IBAction)done:(id)sender 
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

Where done: is linked to the "Done" button via a touch up inside event.

A few things I've tried:

  • Converting the modal segues to push segues fixes the issue, but I don't want a Nav bar in any of the child views
  • I've tried disabling and enabling animations when dismissing the modal view controller (using dismissViewControllerAnimated:)
  • Using unwind segues in the storyboard rather than doing it programmatically

Anyone have an idea of what may be going on?


回答1:


After deleting tons of code, I finally found out that a custom NavigationController was being used and this was the root cause:

@implementation MSLNavigationController

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

@end

The app doesn't need this code, so I've nuked the file. (But an explanation as to why this would be hiding the keyboard would be awesome :))




回答2:


  1. You did not call [super viewDidAppear:animated]
  2. In place like that i have workaround that works pretty well

    - (void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
        {
            if (self.textView.text.isNotEmpty)
            {
                [self.textView becomeFirstResponder];
            }
        });
    }
    



回答3:


I have been struggling with this problem for some time, so I'll post here what I found out.

I was calling textField.becomeFirstResponder() in viewWillAppear but on iOS 7, after the modal was dismissed, the keyboard would not show again, even when you would tap on the textField.

For me calling textField.resignFirstResponder() when the modal is presented, solved the issue. It seems like the input field was already marked as first responder and then would not react to the new calls.



来源:https://stackoverflow.com/questions/25434095/ios-7-keyboard-not-showing-after-leaving-modal-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!