10

我读过关于我如何可以集中一个搜索栏,使当我打开我的搜索视图键盘出现这么多的解决方案,和所有的都是这样专注于的UISearchBar,但键盘没有出现

[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方法再次

感谢执行。

+0

同样的问题我面临的是,我的搜索栏是焦点。模拟光标闪烁,但键盘未显示。你有没有找到解决方案 – 2013-10-29 12:52:31

回答

5

使用UISearchBarDelegate,并宣布在这样的头文件中的搜索栏...

@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]; 

}

声明你的搜索栏,然后使用becomeFirstResponder在您的搜索栏中viewDidAppear

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

}

+2

不应该[超级viewDidAppear:动画];先调用[sBar becomeFirstResponder]; – yunas 2012-12-31 10:25:11

+0

适合我,我在'[super viewDidAppear:animated]之后调用主动搜索栏' – c9s 2015-03-10 03:14:55

7

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

键盘未显示。因此,首先resign 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]; 

}

它的工作原理

+0

这终于为我工作了!有一个upvote。任何想法为什么你必须这样做'[self.searchDisplayController.searchBar becomeFirstResponder];''而不是'[self.searchBar becomeFirstResponder];'? – scottmrogowski 2013-06-24 03:49:07

+0

这工作正常,但我建议只是在'viewDidAppear:'方法内执行'[self callSearchBar]'。我们无法可靠地知道控制器转换可能需要多长时间,所以如果您的延迟太短而无法集中搜索条,您可能会遇到一个难看的效果和/或意外的tableview工件。 +1然而:-) – jweyrich 2013-09-27 03:34:13

+0

@scottmrogowski你必须使用'self.searchDisplayController.searchBar',因为你的'UISearchDisplayController'连接到了你的'UITableViewController',所以你没有在你的'UITableViewController'中有一个searchBar变量“类,但是你可以通过'UISearchDisplayController'变量得到这个引用。 – 2013-12-17 14:13:55

3

序列此事

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

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

3

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

enter image description here