【问题标题】:Focus on the UISearchBar but the keyboard not appear专注于 UISearchBar 但键盘没有出现
【发布时间】:2011-03-29 14:52:45
【问题描述】:

我已经阅读了很多关于如何在打开搜索视图时聚焦搜索栏以使键盘出现的解决方案,所有这些都是这样的

[searchBar becomeFirstResponder];
mine is 
[self.searchDisplayController.searchBar becomeFirstResponder];
but I tried both.

现在,我尝试了这个,我还添加了一个

[self.searchDisplayController setActive:YES];

因为我使用的是 SearchDisplayController,但到目前为止,我能获得的最佳结果是将光标放在搜索栏上,即带有覆盖的 uitableview,但仍然没有键盘。

如果我运行模拟器,我可以用电脑键盘在搜索栏上输入内容,但在 iPhone 上我什么也做不了。

如果你想看看我的代码:http://nopaste.info/39fcda4771.html 焦点应该在 viewDidLoad 方法中执行

再次感谢。

【问题讨论】:

  • 我面临同样的问题,我的 searchBar 是焦点。模拟光标闪烁,但键盘不显示。你找到解决办法了吗

标签: uitableview uisearchbar uisearchdisplaycontroller becomefirstresponder


【解决方案1】:

我在 textFieldDidBeginEditing:(UITextField *)textField 委托方法上显示搜索栏。

键盘未显示。因此,对于第一个将 textField 辞职为 firstResponder。 即

[textField resignFirstResponder];

然后延迟调用方法

[self performSelector:@selector(callSearchBar) withObject:NULL afterDelay:0.2];

在哪里

-(void)callSearchBar{
[self.searchDisplayController setActive: YES animated: YES]; 
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];

}

有效

【讨论】:

  • 这终于对我有用了!投赞成票。知道为什么你必须这样做[self.searchDisplayController.searchBar becomeFirstResponder]; 而不是[self.searchBar becomeFirstResponder];
  • 这很好用,但我建议在viewDidAppear: 方法中简单地执行[self callSearchBar]。我们无法可靠地知道控制器转换可能需要多长时间,因此如果您的延迟太短而无法聚焦searchBar,您最终可能会遇到丑陋的效果和/或意外的 tableview 工件。不过 +1 :-)
  • @scottmrogowski 您必须使用self.searchDisplayController.searchBar,因为您可能已将UISearchDisplayController 连接到您的UITableViewController,因此您的UITableViewController" class, but you can get this reference through the UISearchDisplayController` 变量中没有searchBar 变量.
【解决方案2】:

使用 UISearchBarDelegate 并像这样在头文件中声明 searchBar ...

@interface mySearchScreen : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {

UITableView *myTableView;   
NSMutableArray *tableData;
UISearchBar *sBar;//search bar

并在 loadView 中声明您的搜索栏

- (void)loadView {  
[super loadView];   
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,30)];   
sBar.delegate = self;   
[self.view addSubview:sBar];    
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 31, 300, 400)];   
myTableView.delegate = self;    
myTableView.dataSource = self;  
[self.view addSubview:myTableView];

tableData = [[NSMutableArray alloc]init];

}

然后在 viewDidAppear 的搜索栏中使用 becomeFirstResponder

- (void)viewDidAppear:(BOOL)animated {
//ensure the keyboard comes up from the start
[sBar becomeFirstResponder];    
[super viewDidAppear:animated];

}

【讨论】:

  • 不应该 [super viewDidAppear:animated];比 [sBar becomeFirstResponder] 先调用;
  • 对我有用,我在[super viewDidAppear:animated]之后调用主动搜索栏
【解决方案3】:

顺序很重要

[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];

如果运气不好,则检查搜索显示控制器的委托是否已链接。还值得检查搜索栏的色调。

【讨论】:

    【解决方案4】:

    simulator 上,确保单击Toggle Software Keyboard 以使用模拟器的键盘,如图所示。

    【讨论】:

      猜你喜欢
      • 2010-11-08
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      相关资源
      最近更新 更多