zoukankan      html  css  js  c++  java
  • UISearchBar clearButton

    When the searchBar:textDidChange: method of the UISearchBarDelegate gets called because of the user tapping the 'clear' button, the searchBar hasn't become the first responder yet, so we can take advantage of that in order to detect when the user in fact intended to clear the search and not bring focus to the searchBar and/or do something else.

    To keep track of that, we need to declare a BOOL ivar in our viewController that is also the searchBar delegate (let's call it shouldBeginEditing) and set it with an initial value of YES (supposing our viewController class is called SearchViewController):

    @interface SearchViewController : UIViewController <UISearchBarDelegate> {
        // all of our ivar declarations go here...
        BOOL shouldBeginEditing;
        ....
    }
    
    ...
    @end
    
    
    
    @implementation SearchViewController
    ...
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
            ...
            shouldBeginEditing = YES;
        }
    }
    ...
    @end

    Later on, in the UISearchBarDelegate, we implement the searchBar:textDidChange: andsearchBarShouldBeginEditing: methods:

    - (void)searchBar:(UISearchBar *)bar textDidChange:(NSString *)searchText {
        NSLog(@"searchBar:textDidChange: isFirstResponder: %i", [self.searchBar isFirstResponder]);
        if(![searchBar isFirstResponder]) {
            // user tapped the 'clear' button
            shouldBeginEditing = NO;
            // do whatever I want to happen when the user clears the search...
        }
    }
    
    
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)bar {
        // reset the shouldBeginEditing BOOL ivar to YES, but first take its value and use it to return it from the method call
        BOOL boolToReturn = shouldBeginEditing;
        shouldBeginEditing = YES;
        return boolToReturn;
    }
    

    http://stackoverflow.com/questions/1092246/uisearchbar-clearbutton-forces-the-keyboard-to-appear/3852509#3852509

  • 相关阅读:
    swt 更新主UI线程
    java中 快捷键输入System.out.println();
    原型设计工具
    JAVA笔记
    转:java读取配置文件的几种方法
    Server Message Block
    安全标识符
    BitLocker:如何启用网络解锁
    imageX.exe
    组策略首选项
  • 原文地址:https://www.cnblogs.com/Clin/p/3383779.html
Copyright © 2011-2022 走看看