2012-10-24 29 views
1

所以我在我的頭文件中定義了一個IBAction,在我的main中實現了它,我可以在IB中看到它 - 但是,當我拖過UISearchBar時,它不會連接。IBAction不會拖動到UISearchBar

這裏是我的頭代碼:

#import <UIKit/UIKit.h> 

@class DetailViewController; 

@interface CustomerListViewController : UITableViewController 
{ 
} 

-(IBAction)filterTableData:(UISearchBar *)filterBar; 

@property (strong, nonatomic) DetailViewController *detailViewController; 
@property (retain) IBOutlet UISearchBar *searchBar; 
@property (strong, nonatomic)NSArray *custs, *workingSet; 


@end 

這裏是我的實現方法:

-(IBAction)filterTableData:(UISearchBar *)filterBar { 

    //filter our search results 
    NSString *filter; 
    if (filterBar.text) 
     filter = [filterBar.text stringByAppendingString:@"*"]; 
    else 
     filter = @"*"; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", filter]; 
    self.workingSet = [self.custs filteredArrayUsingPredicate: predicate]; 

    [self.tableView reloadData]; 
} 

而這裏的IB實際看到的方法,並允許它被拖動的圖象,並且我沒有成功嘗試將其拖動到搜索欄:

http://i.imgur.com/QLPMT.png

回答

2

UISearchBar不是UIControl的子類,這意味着您不能添加要在發生事件時調用的方法。

你想要做的就是將你的視圖控制器設置爲UISearchBarDelegate並使用下面的方法而不是你的filterTableData:方法。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

然後在IB你要點擊和搜索欄,以文件的所有者阻力和選擇委託財產。

+0

令人驚歎 - 像魅力一樣工作。在適當的時間過後,會接受。 – Darrrrrren

+0

我還會包含對「 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar」的調用。這將允許您刪除鍵盤並在用戶完成輸入時處理完整的字符串。用[searchBar resignFirstResponder];移除鍵盤; – JeffB6688

相關問題