聚焦于 UISearchBar 但键盘不出现

发布于 2024-10-27 21:58:51 字数 617 浏览 0 评论 0原文

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

[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 方法中执行

再次感谢。

I've read so many solution on how can i focus a searchbar to make keyboard appear when i open my search view, and all of that are like this

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

Now, I tried this, and I also added a

[self.searchDisplayController setActive:YES];

because I'm using a SearchDisplayController, but so far the best result i can have is to have the cursor on the searchbar, the uitableview with an overlay on it, but still no keyboard.

If I run the simulator I can type on the searchbar with my computer's keyboard, but on an iPhone I can't do anything.

If you want to give a look to my code: http://nopaste.info/39fcda4771.html
the focus should be executed in viewDidLoad method

Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三岁铭 2024-11-03 21:58:51

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

键盘没有显示。因此,首先将 textField 作为第一个响应者。

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

}

它有效

I was showing searchbar on textFieldDidBeginEditing:(UITextField *)textField delegate method.

Keyboard was not showing. So for that first resign textField as firstResponder.
i.e.

[textField resignFirstResponder];

Then call method with delay

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

Where

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

}

It works

懒猫 2024-11-03 21:58:51

使用 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 中的搜索栏上使用 BebeFirstResponder

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

}

Use the UISearchBarDelegate and declare the searchBar in the header file like this ...

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

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

and declare your search bar in 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];

}

and then use becomeFirstResponder on your search bar in viewDidAppear

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

}

无人接听 2024-11-03 21:58:51

顺序很重要

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

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

The sequence matter

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

If no luck then check if delegate of the search display controller is linked. Also worth checking the tint colour of the search bar.

烟酉 2024-11-03 21:58:51

模拟器上,确保单击切换软件键盘以使用模拟器的键盘,如图所示。

在此处输入图像描述

On the simulator, make sure that you click on Toggle Software Keyboard to use the keyboard of the simulator as you can see in the image.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文