은 여기, 내 응용 프로그램에 대한 사용자 지정 검색 창을 만든 클래스입니다 : 나는 searchBarSearchButtonClicked:
대리자 메서드의 기능을 얻으려고사용자 정의 UISearchBar로 searchBarDelegate 메소드를 얻으려면 어떻게해야합니까?
@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;
}
.H
@interface SearchView : UIView <UITextFieldDelegate, UISearchBarDelegate>
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchButton;
@end
하는 .m은. 여기
은 (taskListViewController)에 대한 코드입니다- (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을 설정 .H
@interface TaskListViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate, UITextFieldDelegate, ScavengerAPIGetTaskListDelegate>{
SearchView *searchView;
}
하는 .m
. uisearchBar 대신 uiview를 서브 클래 싱하기 때문에 호출되지 않는 이유가 무엇입니까? 더 많은 코드/세부 정보가 필요하면 알려주십시오! 감사!
당신은이 클래스는 차별화 코드를 더 읽기 쉽게하기 위해 기존 UISearchBar를 사용하는 위임의 자체 검색 방법을 대신 구현할 수 있도록하는 것이 좋습니다 있도록 사용자 정의 검색보기를 만든