Ios 以编程方式创建可从多个视图使用的UISearchBar
我想创建一个UISearchBar,可以在我的应用程序的多个视图中查看和使用 因此,我在AppDelegate类中声明了一个UISearchBar属性,并在我的第一个UIViewController中对其进行了初始化Ios 以编程方式创建可从多个视图使用的UISearchBar,ios,objective-c,Ios,Objective C,我想创建一个UISearchBar,可以在我的应用程序的多个视图中查看和使用 因此,我在AppDelegate类中声明了一个UISearchBar属性,并在我的第一个UIViewController中对其进行了初始化 @interface AppDelegate : UIResponder <UIApplicationDelegate>{ IBOutlet UIWindow *window; } @property (strong, nonatomic) IBOutle
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
IBOutlet UIWindow *window;
}
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
然而,当搜索栏初始化并在VC上显示良好时,搜索机制不起作用。我可以在搜索文本框中书写,但没有任何操作。甚至键盘上的“搜索”按钮也不起作用。您必须根据使用方式在相应的控制器类中实现
UISearchBarDelegate
方法
参考这个
扩展答案:
在“my first view.m”文件中,您需要实现UISearchBarDelegate方法以获得操作
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//This is where your search button action fires. You can implement what you want to do after search button click in here.
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
//This fires whenever you change text in your search bar. For an example if you want to show search suggestions when a new letter is typed you can implement that in here.
}
您可以根据需要使用其他代理方法。在XIB中使用搜索栏和搜索显示控制器对象。只需在XIB中拖放此对象 并实现了表视图委托和数据源方法 像 搜索显示控制器方法
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
if(self.searchCategories == nil){
NSMutableArray *arr = [[NSMutableArray alloc] init];
self.searchCategories = arr;
[arr release];
}else{
[self.searchCategories removeAllObjects];
}
for (NSString *aStr in self.categories) {
if([[aStr lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound){
[self.searchCategories addObject:aStr];
}
}
[controller.searchResultsTableView reloadData];
return NO;
}
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
[self.aTableView setHidden:YES];
}
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
[self.aTableView setHidden:NO];
}
希望这对你有帮助。如果您仍然有任何问题,我建议您阅读
UISearchDisplayController
您在哪里实现searchBarDelegate方法?请您使用一些示例,因为这是我第一次使用search barOk,我将改进我的答案。谢谢。但是,仍然没有行动!使用
searchBarSearchButton中的NSLog
单击和searchBarTextDidBeginEditing
,但它们不会打印出来。正如您所看到的,我将searchResultsDelegate
和searchResultsDataSource
交给了self。当我试图使用属于另一个类的UISearchBar对象时,我是否做错了什么searchBar
?当然,您必须将其分配给视图控制器中声明的属性。如果需要,可以在app delegate中分配它,并将其分配给当前视图控制器中定义的属性。但是像这样尝试appDelegate.searchBar.delegat=self;在您的视图控制器中尝试这样分配后,这可能会起作用。谢谢。然而,如果我想在每个VC上都有搜索栏,我必须为每个视图都这样做吗?有没有办法创建一个“全局”搜索栏?是的,你可以为每个视图控制器创建一个“全局”搜索栏,但你可以创建自己的自定义搜索栏,但我建议使用这个搜索显示控制器,因为它很容易添加(只需拖放到xib并实现三种方式)看起来很酷,也很容易使用。你们知道我如何修改搜索栏的位置吗?我想有一个简单的动画显示/隐藏搜索栏。最初,搜索栏位于导航栏后面。动画效果很好,但当我尝试搜索时,searchResultsTableView
会出现,导航栏和搜索栏一起出现在屏幕外。你知道我如何修复它吗?你应该把搜索结果显示对象放在导航栏后面。选择所需的xib->选择视图->选择属性检查器->选择顶部栏属性并放置导航栏。就这些。默认情况下,您将获得很酷的动画。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == self.aTableView)
return [self.categories count];//normal table view
return [self.searchCategories count];//search display controllers TableView
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"reusableCells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
NSString *text = nil;
if(tableView == self.aTableView)
text = [self.categories objectAtIndex:indexPath.row];
else
text = [self.searchCategories objectAtIndex:indexPath.row];
cell.textLabel.text = text;
return cell;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
if(self.searchCategories == nil){
NSMutableArray *arr = [[NSMutableArray alloc] init];
self.searchCategories = arr;
[arr release];
}else{
[self.searchCategories removeAllObjects];
}
for (NSString *aStr in self.categories) {
if([[aStr lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound){
[self.searchCategories addObject:aStr];
}
}
[controller.searchResultsTableView reloadData];
return NO;
}
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
[self.aTableView setHidden:YES];
}
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
[self.aTableView setHidden:NO];
}
UISearchDisplayController