2009-11-11 59 views
1

我有一個頂部的搜索欄的桌面視圖。當我點擊搜索時,它被呼叫到第一響應者並且鍵盤出現。搜索欄有一個取消按鈕,但是當表格爲空時,我希望用戶能夠點擊空表格行上的任意位置,以便將搜索欄和鍵盤作爲第一響應者。我該怎麼做呢?點擊空的UITableView來退出搜索欄

回答

4

您將不得不創建自己的覆蓋touchesEnded方法的UITableView類。然後你可以回叫你的委託人,委託人應該是你的視圖控制器,並告訴它告訴你的UISearchBar辭去第一響應者。

的UITableView的派生類會是這個樣子:

#import <UIKit/UIKit.h> 

@interface FRTableView : UITableView { 
} 

@end 

// ------------------------------ 

#import "FRTableView.h" 

@implementation FRTableView 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesEnded:touches withEvent:event]; 
    NSLog(@"Touches ended"); 

    // Bubble back up to your view controller which should be this 
    // table view's delegate. 
    if ([self delegate] && [[self delegate] respondsToSelector:@selector(touchEnded)]) 
     [[self delegate] performSelector:@selector(touchEnded)]; 
} 

@end 

請務必設置在Interface Builder你的表視圖中使用自定義類,而不是正常的UITableView類的。然後在您的視圖控制器,實現選擇,我叫touchEnded這樣的:

- (void)touchEnded; 
{ 
    NSLog(@"Back in root view controller."); 
    [searchBar resignFirstResponder]; 
} 

在哪裏搜索欄是連接在Interface Builder一個UISearchBar一個IBOutlet。

0

Ÿ不ü嘗試...... UISearchDisplayController

只要定義這些在烏拉圭回合.h文件中

IBOutlet中的UISearchBar * mySearchBar; UISearchDisplayController * mySearchDisplayController; IBOutlet UITableView * myTableView;

和.H

現在在.m文件

處理UISearchBarDelegate只寫這些代碼

mySearchDisplayController = [[UISearchDisplayController的alloc] initWithSearchBar:mySearchBar contentsController:自]; mySearchDisplayController.searchResultsDelegate = self; mySearchDisplayController.searchResultsDataSource = self; mySearchDisplayController.delegate = self;

我希望這可以幫到.......