简体   繁体   English

为什么UISearchBar即使在被设置为第一响应者之后也没有获得焦点?

[英]Why UISearchBar does not get Focus even after it is set as a first responder?

Why UISearchBar does not get Focus and keyboard does not appear when UIViewController is pushed to navigation controller for the 2nd time? 当UIViewController第二次被推送到导航控制器时,为什么UISearchBar没有得到Focus并且键盘没有出现? It seems as no element on the whole view has a focus. 似乎整个视图中的任何元素都没有焦点。

MY USE CASE : I have 2 View Controllers: A and B. Transition is A->B. 我的用例 :我有2个视图控制器:A和B.转换是A-> B. A, B are pushed to navigation controller. A,B被推送到导航控制器。

  1. When I show B for the first time focus is set to the searchbar and keyboard appears.OK. 当我第一次显示B 时,焦点设置为搜索栏并且键盘出现.OK。
  2. Then I go back to the A. And again from A->B. 然后我回到A.再次从A-> B。 Now for the second time there is no focus and keyboard does not appear . 现在第二次 没有焦点键盘也没出现 It is WRONG. 这是错误的。

MY CODE : On the B UIViewController IBOutlet connection to the searchbar is created: 我的代码 :在B UIViewController上创建了与搜索栏的IBOutlet连接:

@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar;

Delegate of the mySearchBar is also set. mySearchBar的代表也已设置。 I set focus on the searchbar in B UIViewController in viewDidAppear method: 我在viewDidAppear方法中将焦点放在B UIViewController中的搜索viewDidAppear

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];   
    [self performSelector:@selector(setCorrectFocus) withObject:NULL afterDelay:0.2];
}

-(void) setCorrectFocus {
     [self.mySearchBar becomeFirstResponder];
}

Transitions between controllers are done manually in code like this: 控制器之间的转换是通过以下代码手动完成的:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
AController *a = [storyboard instantiateViewControllerWithIdentifier:@"A"];
[self.navigationController pushViewController:a animated:NO];

Try this 试试这个

-(void)viewDidAppear:(BOOL)animated
{
     [super viewDidAppear:animated];   
     [self setCorrectFocus];
}

-(void)setCorrectFocus 
{
     [self.mySearchBar becomeFirstResponder];
}

After long searching I found Error. 经过长时间的搜索,我发现了错 Somewhere in storyboard old connection for UISearchBar with the name mySearchbar was left. 在故事板的某处,UISearchBar的旧连接名为mySearchbar。 Another thing that I had to correct was that the method 我必须纠正的另一件事是该方法

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

returned NO , every time I went back to previous Controller. 每次我回到之前的控制器时都返回NO But it should return YES to be able to set focus on search bar next time. 但它应该返回YES以便下次能够将焦点设置在搜索栏上。

Was strugling with the same - had a UISearchBar inside a UITextView's keyboard accessory and even after [self.searchBar becomeFirstResponder] was called, keyboard input was still received by the text view. 与之相似 - 在UITextView的键盘附件中有一个UISearchBar,甚至在调用[self.searchBar becomeFirstResponder]之后,文本视图仍然接收到键盘输入。

This is what finally helped: 这是最终帮助的:

[self.searchBar.window makeKeyAndVisible];

Apparently the keyboard accessory is part of a different UIWindow than the UITextField. 显然,键盘附件是与UITextField不同的UIWindow的一部分。

Note: To continue typing in the text view, you should then call something like [self.textView.window makeKeyAndVisible]; 注意:要继续在文本视图中键入,您应该调用类似[self.textView.window makeKeyAndVisible]; ... ...

你有没有尝试过添加self.mySearchBar.delegate = self;

You should use strong in your property 你应该在你的财产中使用strong

@property (strong, nonatomic) IBOutlet UISearchBar *mySearchBar;

Always set strong in your property for IBOutlet. 始终为您的财产设置强大的IBOutlet。 When you pop B from navigationController, mySearchBar has released, so you push back to B, [self.mySearchBar becomeFirstResponder] is not work. 当您从navigationController弹出B时,mySearchBar已经释放,所以你回到B,[self.mySearchBar becomeFirstResponder]不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的UISearchBar在成为第一响应者之后的宽度为0? - Why is my UISearchBar's width 0 after becoming first responder? 解除弹出框后的第一响应者/焦点 - First responder/focus after dismissing popover 当UISearchBar不再是第一响应者时,是否有委托通知您? - Does UISearchBar have a delegate that notifies you when it is no longer the first responder? 嵌入在导航栏中的UISearchController的UISearchBar:不成为第一响应者 - UISearchController's UISearchBar embedded in Navigation Bar: Does not become first responder UIPopoverController中的UISearchBar没有得到第一响应者 - UISearchBar in UIPopoverController not getting first responder 响应者在UISearchBar上平移手势 - Pan gesture on UISearchBar while first responder iOS 7 UISearchBar是第一个响应者时的自定义 - iOS 7 UISearchBar customization when it is first responder UITextField成为第一响应者后,UITableView不会将单元格滚动到可见 - UITableView does not scroll cell to visible after UITextField becomes first responder 当 UITextView 成为第一响应者时,为什么 UIScrollView 不向上滚动? - Why UIScrollView does not scroll up when UITextView becomes first responder? 尝试选择UICollectionViewCell时,UISearchBar退出第一响应者 - UISearchBar resigns first responder when trying to select UICollectionViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM