按下' x'按钮?
我尝试这种方法但不起作用。
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
}
答案 0 :(得分:19)
我认为没有一种简单的方法可以挂钩X按钮。
但是,您可以直接考虑其结果:清除文本。
试试这个:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
print("UISearchBar.text cleared!")
}
}
副作用是手动清除文本时也会调用它。
答案 1 :(得分:10)
Swift 5.2,Xcode 11.4
这个对我有用。
if let searchTextField = self.categoriesSearchBar.value(forKey: "searchField") as? UITextField , let clearButton = searchTextField.value(forKey: "_clearButton")as? UIButton {
clearButton.addTarget(self, action: #selector(self.yourFunction), for: .touchUpInside)
}
答案 2 :(得分:7)
一种相当古怪的方法,但是可行。
private var didTapDeleteKey = false
func searchBar(_ searchBar: UISearchBar,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool
{
didTapDeleteKey = text.isEmpty
return true
}
func searchBar(_ searchBar: UISearchBar,
textDidChange searchText: String)
{
if !didTapDeleteKey && searchText.isEmpty {
// Do something here
}
didTapDeleteKey = false
}
此代码的成功取决于以下事实:点击搜索栏的清除按钮时,不会调用searchBar(_:shouldChangeTextIn:replacementText:) -> Bool
。取而代之的是,当点击键盘上的删除按钮时,将调用该方法。
答案 3 :(得分:3)
我遇到了同样的问题,试图通过访问searchBar的子视图textField解决它,但它导致我的应用程序崩溃,并且在最后的努力中我找到了这个post。
另一个问题是,
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
被调用两次,另请参阅this。
最后我做的是;
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (![searchText isEqualToString:self.searchText]) {
self.searchText = searchText;
return;
}
NSLog(@"Clear clicked");
}
这解决了我的问题。
答案 4 :(得分:2)
这对我有用
-(UITextField*)getUITexfieldInUISearchBar:(UIView*)aSearchBar{
UITextField *searchBarTextField;
for (UIView *subView in [aSearchBar subviews]){
if ([subView isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subView;
[searchBarTextField setDelegate:self];
break;
}else if ([subView isKindOfClass:[UIView class]]){
[self getUITexfieldInUISearchBar:subView];
}
}
return searchBarTextField;
}
您可以使用TextFields Delegate
- (BOOL)textFieldShouldClear:(UITextField *)textField {
//Do something here...
}
答案 5 :(得分:1)
您可以通过符合“UISearchBarDelegate”连接到“取消按钮”单击事件,然后实现“searchBarCancelButtonClicked”方法
答案 6 :(得分:1)
我认为这个问题最简单的解决方案是:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
答案 7 :(得分:0)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
print("searchText: \(searchText)")
if searchText.count > 0
{
// Show ur content
}
else
{
// Hide your content
}
}
答案 8 :(得分:0)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
//clear button (x) on the search bar is cliecked, so clear the table
[self clearTable];
}
}
答案 9 :(得分:0)
...正常工作
Traceback (most recent call last):
File "modeltraining.py", line 29, in <module>
sr,audio = read(source + path)
File "C:\Users\RAAM COMPUTERS\Anaconda3\lib\site-packages\scipy\io\wavfile.py", line 233, in read
fid = open(filename, 'rb')
PermissionError: [Errno 13] Permission denied: 'development_set/'
答案 10 :(得分:0)
Swift 5.0
中最简单的解决方案func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
searchBar.resignFirstResponder()
}
}
}
答案 11 :(得分:0)
self.mySearchBar.searchTextField.delegate =自我
扩展名myViewController:UISearchTextFieldDelegate {
func textFieldShouldClear(_ textField: UITextField) -> Bool {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.mySearchBar.searchTextField.resignFirstResponder()
}
return true
}
}
答案 12 :(得分:0)
按下搜索栏上的十字按钮时,将调用以下方法:
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
}
答案 13 :(得分:0)
直到苹果发布正确的委托方法来处理清除按钮事件,此方法将从Xcode 12开始生效。要求:> = iOS13
尽可能获取对searchBar clearButton
的引用。
UIButton *searchBarTextFieldClearButton = [self.searchBar.searchTextField valueForKey:@"_clearButton"];
if (searchBarTextFieldClearButton) {
[searchBarTextFieldClearButton addTarget:self action:@selector(searchTextFieldClearButtonTapHandler) forControlEvents:UIControlEventTouchUpInside];
}
迅速:
extension UISearchTextField {
var clearButton: UIButton? {
return value(forKey: "_clearButton") as? UIButton
}
}
searchBar.searchTextField.clearButton.addTarget(self, #selector(handler), .touchUpInside) //bllah
请务必记住,您的“操作-searchTextFieldClearButtonTapHandler
”是最后一次调用,这一点非常重要。也就是说,在委托方法- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
答案 14 :(得分:-1)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let inputString = searchText.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
if inputString.characters.count > 0
{
}
else
{
//Write code here for Clear button action
}
}