스마트폰

x button delegate of UISearchBar

프로채터 2011. 10. 26. 11:05
UISearchBarDelegate에서 showsCancelButton=NO로 생성을 해도
searchBarCancelButtonClicked가 먹힐줄 알았는데 안먹히네요... 다행히 하위뷰를 검색해서
UITextField를 강제로 delegate를 self로 먹이니 바로 해결되었습니다.
x버튼을 누르면 UITextField에 델리게이트가 동작하네요...
단! 그밖의 delegate는 UISearchBar에서 동작이 중복될수도 있으니 주의하시길. 
 
- (void)viewDidLoad:
{
    for (UIView* v in searchBar.subviews)
    {
        if ( [v isKindOfClass: [UITextField class]] )
        {
            UITextField *tf = (UITextField *)v;
            tf.delegate = self;
            break;
        }
    }    
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
     [textField resignFirstResponder];
     [self.searchBar resignFirstResponder];
     return YES;
}