- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为我的应用程序制作了一个自定义搜索栏,这是该类:.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:
委托(delegate)方法的功能。
这里是代码(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 吗?如果您需要更多代码/详细信息,请告诉我!谢谢!
最佳答案
您已经创建了一个自定义搜索 View ,因此建议让您的类实现自己的委托(delegate)搜索方法,而不是使用现有的 UISearchBar 来区分并使代码更具可读性。
您应该为您的 SearchView
类创建一个委托(delegate)
在这里你可以引用如何创建你自己的 How to make Custom Delegate in iOS app
创建 TaskListViewController
后可以符合该委托(delegate),您可以在点击 searchButton 时调用 searchview 内的方法,以便 TaskListViewController 接收它
关于iphone - 如何使用自定义 UISearchBar 获取 searchBarDelegate 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454694/
我为我的应用程序制作了一个自定义搜索栏,这是该类:.h @interface SearchView : UIView @property (nonatomic, strong) UITextFiel
我是一名优秀的程序员,十分优秀!