0

我为我的应用程序制作了一个自定义搜索栏,这是类:.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?如果您需要更多代码/详细信息,请告诉我!谢谢!

4

1 回答 1

1

您已经创建了一个自定义搜索视图,因此建议让您的类实现自己的委托搜索方法,而不是使用现有的 UISearchBar 来区分并使代码更具可读性。

SearchView你应该为你的班级做一个代表

在这里您可以参考如何创建自己的 如何在 iOS 应用中制作自定义委托

创建后TaskListViewController可以符合该委托,您可以在点击 searchButton 时调用 searchview 内的方法,以便 TaskListViewController 接收它

于 2013-10-18T16:38:06.450 回答