简体   繁体   English

单击按钮时如何激活UISearchDisplayController

[英]How do you activate a UISearchDisplayController when a button is clicked

I have this UISearchDisplayController and so far it can only be activated (ready to type with a drop down searchresulttableview ) when someone clicks the UISearchBar . 我有这个UISearchDisplayController ,到目前为止,只有当有人单击UISearchBar时,它才能被激活(准备使用下拉的searchresulttableview键入)。 Is there a way to make a button do this task? 有没有办法使按钮执行此任务? I will appreciate any answer I receive. 我将不胜感激。

您是否在按钮选择器中尝试过[searchDisplayController.searchbar becomeFirstResponder]

Look at the docs for UISearchDisplayController . 查看UISearchDisplayController的文档。 There is an active property and a setActive:animated: method. 有一个active属性和一个setActive:animated:方法。

Have your button action call this: 让您的按钮操作将此称为:

[theSearchController setActive:YES animated:YES];

Since UISearchBar extends UIResponder just like a UITextField , you can simply call becomeFirstResponder on the search bar to give it focus. 由于UISearchBar就像UITextField一样扩展了UIResponder ,因此您可以简单地在搜索栏上调用becomeFirstResponder使其成为焦点。

[button addTarget:self action:@selector(clickedSearchBar) forControlEvents:UIControlEventTouchUpInside];

- (void)clickedSearchBar {
    [searchDisplayController.searchbar becomeFirstResponder];
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM