[英]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被推送到导航控制器。
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.