通过 becomeFirstResponder 将焦点分配给 UISearchController 的 UISearch

时间:2023-01-30
本文介绍了通过 becomeFirstResponder 将焦点分配给 UISearchController 的 UISearchBar 时,键盘不出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我花了很多时间在线搜索并与其他开发人员讨论此问题,但无济于事.此 SO 帖子中描述了确切的问题(关注 UISearchBar但键盘没有出现),虽然它已经很多年了.

I've spent quite a bit of time searching online and talking to other developers about this issue to no avail. The exact issue is described in this SO post (Focus on the UISearchBar but the keyboard not appear), although it's many years old.

我最近在 IB 中不再使用已弃用的 UISearchDisplayController 和 UISearchBar,而是通过 iOS8 的代码切换到 UISearchController.

I recently switched from using the deprecated UISearchDisplayController and UISearchBar in IB, and switched over to UISearchController via the code for iOS8.

然而,我遇到的问题是焦点分配正确(您可以看出,因为取消按钮在视图加载后会在搜索栏的右侧动画),但是键盘没有显示.

The problem I'm getting however, is that focus is assigned correctly (you can tell because the cancel button animates to the right of the search bar after the view loads), however the keyboard does not show up.

这是我的代码.

.h

@property (nonatomic, strong) UISearchController *searchController;

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [self initializeSearchController];
    ....
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.searchController setActive:YES];
    [self.searchController.searchBar becomeFirstResponder];
}

- (void)initializeSearchController {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.delegate = self;
    self.searchController.searchBar.delegate = self;
    [self.searchController.searchBar sizeToFit];

    [self.tableView setTableHeaderView:self.searchController.searchBar];
    self.definesPresentationContext = YES;
}

到目前为止我尝试过的事情.

The things I've tried so far.

  • 我尝试过延迟 0.2 秒调用 becomeFirstResponder,正如另一篇 SO 帖子中所建议的那样.

  • I've tried calling becomeFirstResponder on a 0.2 second delay, as suggested in another SO post.

我在 viewDidAppear 中设置了一个断点,并验证 self.searchController 和 self.searchController.searchBar 都是有效对象,都不是 nil.

I've set a breakpoint in viewDidAppear, and verified that both self.searchController and self.searchController.searchBar are both valid objects, neither nil.

我已尝试符合 UISearchControllerDelegate 并使用以下代码片段

I've tried conforming to the UISearchControllerDelegate and using the following snippet of code

这里:

- (void)didPresentSearchController:(UISearchController *)searchController {
    //no matter what code I put in here to becomeFirstResponder, it doesn't
    //matter because this is never called, despite setting the     
    //self.searchController.delegate = self AND 
    //self.searchController.searchBar.delegate = self.
}

  • 我在情节提要中从头开始创建了一个新视图,并改为使用该视图,以确保我的视图中没有一些旧的 searchBar 残余.这也不起作用.

    • I've created a new view from scratch in storyboards, and segued to that one instead, to make sure I didn't have some old searchBar remnant in my view. This did not work either.

      我只在真机(iPhone 6)上测试过,不显示键盘不是模拟器问题.

      I've only tested this on a real device (iPhone 6), and it's not a simulator issue of not showing the keyboard.

      我没有想法,我已经在网络上看到了与此相关的所有问题和答案.没有任何效果.

      I'm out of ideas, and I've seen every question and answer related to this one the web. Nothing is working.

      为了再次澄清发生了什么,searchBar 正确地成为了第一响应者,它右侧的取消按钮在屏幕上显示动画来证明这一点,但键盘没有出现,并且光标在 searchBar 中没有闪烁.

      To clarify again what's going on, the searchBar correctly becomes the first responder, the cancel button to the right of it animates onscreen proving this, but the keyboard does not appear and the cursor does not blink in the searchBar.

      推荐答案

      你的代码看起来没问题.你所描述的不是正常行为.您可以做的第一件事是创建一个只有 UISearchController 功能的新项目,然后看看它是如何进行的.您可以使用它编辑您的问题,以便我们更好地查看.

      Your code looks ok. What you are describing isn't normal behaviour. The first thing you can do is to create a new project with just the UISearchController functionality and see how it goes. You can edit your question with it so we'll have a better view.

      这里有一个关于如何实现 UISearchController 的好例子:Sample-UISearchController

      There's a good example on how to implement UISearchController here: Sample-UISearchController

      添加:

      -(void)viewDidAppear:(BOOL)animated
      {
          [super viewDidAppear:animated];
          [self.searchController.searchBar becomeFirstResponder];
      }
      

      to MasterViewController_TableResults.m 给出了预期的结果,并且键盘在 iPad 上启动时弹出.装有 iOS 8.3 的 iPhone.

      to MasterViewController_TableResults.m gave the expected results and the keyboard popped up on launch on an iPad & iPhone with iOS 8.3.

      你可以回顾一下那个项目,看看你做了什么不同的事情,

      You can go over that project and see what you did differently,

      显然,如果 [self.searchController setActive:YES]becomeFirstResponder 之前调用,则键盘不会显示.我想知道这是否是一个错误.

      Apparently if [self.searchController setActive:YES] is called before becomeFirstResponder the keyboard won't show. I wonder if that's a bug or not.

      这篇关于通过 becomeFirstResponder 将焦点分配给 UISearchController 的 UISearchBar 时,键盘不出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:让我的 Cocoa 应用程序响应键盘播放/暂停键? 下一篇:出现键盘时调整视图大小 (iOS)

相关文章

最新文章