我为我的应用程序制作了一个自定义搜索栏,这是类:.h
@interface SearchView : UIView <UITextFieldDelegate, UISearchBarDelegate>
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchButton;
@end
.m
@implementation SearchView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect frame = CGRectMake(0, 0, 35, 35);
[self.searchButton sizeToFit];
self.alpha = 0.6;
self.backgroundColor = [UIColor colorWithRed:206/255.0 green:206/255.0 blue:206/255.0 alpha:.8];
self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[self.searchButton addTarget:self action:@selector(performSearchQuery) forControlEvents:UIControlEventTouchUpInside];
[self.searchButton setImage:[UIImage imageNamed:@"SearchIcon2"] forState:UIControlStateNormal];
//[self.searchButton sizeToFit];
//self.searchButton.bounds.size.height = ;
self.searchButton.frame = frame;
// self.searchButton.right = frame.size.width - kPadding;
[self addSubview:self.searchButton];
self.searchField = [[UITextField alloc] initWithFrame:CGRectMake(43, -5, 270, 50)];
//self.searchField.placeholder = [@"Search..." uppercaseString];
self.searchField.delegate = self;
self.searchField.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.searchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.searchField.returnKeyType = UIReturnKeySearch;
self.searchField.clearsOnBeginEditing = YES;
self.searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self addSubview:self.searchField];
}
return self;
}
我正在尝试获取searchBarSearchButtonClicked:
委托方法的功能。
这是(taskListViewController)的代码:
。H
@interface TaskListViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate, UITextFieldDelegate, ScavengerAPIGetTaskListDelegate>{
SearchView *searchView;
}
.m
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
searchBar = (UISearchBar *)searchView;
[self didTapSearchButton];
}
- (IBAction)didTapSearchButton
{
NSString *searchTerm = searchView.searchField.text;
if ([searchTerm isEqualToString:@""]) {
[self.view endEditing:YES];
return ;
}
[searchView resignFirstResponder];
NSArray *results = [[CoreDataManager sharedInstance] fetchTaskByName:searchTerm];
[tasks removeAllObjects];
[tasks addObjectsFromArray:results];
[self.view endEditing:YES];
[taskTable reloadData];
}
所以我在 searchView 类和 TaskListViewController 上都设置了 uisearchBarDelegate。它没有被调用的原因是因为我是子类化 uiview 而不是 uisearchBar?如果您需要更多代码/详细信息,请告诉我!谢谢!