如何在不激活 UISearchDisplayController 的情况下在 UISearchBar 中设置文本
我在我的应用程序中使用 UISearchDisplayController。当用户在返回的搜索结果中选择一个项目时,我停用 UISearchDisplayController。停用控制器会清除用户输入的文本。我想把它留在那里。我可以通过在控制器停用后再次设置文本来将文本强制返回到 UISearchBar 中。
就像这样:
NSString* searchText = self.searchDisplayController.searchBar.text;
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = searchText;
哪个有效。
但是,如果我不为停用调用设置动画,我会遇到计时问题。呼唤 像这样 setActive:
NSString* searchText = self.searchDisplayController.searchBar.text;
[self.searchDisplayController setActive:NO animated:NO];
self.searchDisplayController.searchBar.text = searchText;
使 UISearchDisplayController 再次变为活动状态!
有没有一种方法可以设置 UISearchBar 的文本,而无需激活与之关联的 UISearchDisplayController?还有其他建议来解决此行为吗?
I'm using a UISearchDisplayController in my app. When the user selects an item in the search results returned I deactivate the UISearchDisplayController. Deactivating the controller clears the text the user has typed. I want to keep it there. I can force the text back into the UISearchBar by setting it again after the controller has been deactivated.
Like so:
NSString* searchText = self.searchDisplayController.searchBar.text;
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = searchText;
Which works.
However, I am seeing a timing issue if I don't animate the deactivate call. Calling
setActive like so:
NSString* searchText = self.searchDisplayController.searchBar.text;
[self.searchDisplayController setActive:NO animated:NO];
self.searchDisplayController.searchBar.text = searchText;
causes the UISearchDisplayController to become active again!
Is there are a way that I can set the text of the UISearchBar without having the UISearchDisplayController that's associated with become active? Any other suggestions to get around this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于任何其他想知道如何做到这一点的人,我通过在我的委托中添加以下内容来设法使其工作:
For any one else wondering how to do this I managed to get it working by adding this in my delegate:
亚伦的回答很好。一种更简单的方法,通过编辑代码:
这样,在设置搜索栏文本时不会触发任何委托函数。
Aaron answer works fine. A simpler way of doing things, by editing your peace of code:
That way, none of your delegate functions are going to be triggered when setting the search bar text.
在Apple的内部论坛中,有人提出了一种解决方法,即在UISearchDisplayController停用时将searchBar的占位符文本设置为最后一个搜索文本。它出现在框中,但呈灰色。不理想,但可能可以接受。
In Apple's internal forum someone suggested a workaround of setting the placeholder text of the searchBar to the last search text when the UISearchDisplayController deactivates. It appears in the box, but it's greyed out. Not ideal, but possibly acceptable.