gpt4 book ai didi

iphone - 如何使用自定义 UISearchBar 获取 searchBarDelegate 方法?

转载 作者:行者123 更新时间:2023-11-29 03:33:42 24 4
gpt4 key购买 nike

我为我的应用程序制作了一个自定义搜索栏,这是该类:.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com