UISearchBar 没有响应

Posted

技术标签:

【中文标题】UISearchBar 没有响应【英文标题】:UISearchBar not responding 【发布时间】:2017-05-04 10:01:43 【问题描述】:

我在UITableView 中添加了UISearchBar,然后在UIView 中添加了它。我在窗口上添加UIView,一切正常。 searchBarShouldBeginEditing 正在触发并显示日志,但是 searchBarTextDidBeginEditing 没有触发。以下是我的代码:

AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
self.frame = appDelegate.window.frame;
[appDelegate.window addSubview:self];

myView.h内:

@property(strong, nonatomic) UISearchController *searchController;

myView.m内部:

- (void)drawRect:(CGRect)rect 
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    [self.searchController.searchBar sizeToFit];
    self.searchController.searchBar.userInteractionEnabled = YES;
    tblDropdown.tableHeaderView = self.searchController.searchBar;

    arrSeached = [NSMutableArray array];

【问题讨论】:

google.com/… drawRect 不能用于添加仅用于绘图的视图。如果要添加子视图,可以在初始化程序或视图控制器的 viewDidLoad 中进行 【参考方案1】:

也许,问题在于您的视图控制器或视图(我知道没有所有代码)没有正确添加协议。

无论如何,这是一个使用 UISearchController 的示例:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>

@property (nonatomic, weak) IBOutlet UITableView * tableView;
@property (nonatomic, strong) UISearchController * searchController;
@property (nonatomic, strong) NSMutableArray * allItems;
@property (nonatomic, strong) NSMutableArray * filteredItems;
@property (nonatomic, weak) NSArray * displayedItems;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize tableView;
@synthesize searchController;
@synthesize allItems;
@synthesize displayedItems;
@synthesize filteredItems;

- (void)viewDidLoad 
     [super viewDidLoad];

     // Create a list

     self.allItems = [[NSMutableArray alloc] init];
     [self.allItems addObject:@"One"];
     [self.allItems addObject:@"Two"];
     [self.allItems addObject:@"Three"];


     // Create a list to hold search results (filtered list)
    self.filteredItems = [[NSMutableArray alloc] init];

    // Initially display the full list.  This variable will toggle between the full and the filtered lists.
    self.displayedItems = self.allItems;

    // Here's where we create our UISearchController

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;

    [self.searchController.searchBar sizeToFit];

    // Add the UISearchBar to the top header of the table view
    self.tableView.tableHeaderView = self.searchController.searchBar;

    // Hides search bar initially.  When the user pulls down on the list, the search bar is revealed.
    [self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height)];


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
    return [self.displayedItems count];


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)anIndexPath 

    UITableViewCell * cell = [aTableView dequeueReusableCellWithIdentifier:@"FruitCell"];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] init];
    
    cell.textLabel.text = [self.displayedItems objectAtIndex:anIndexPath.row];
    return cell;


// When the user types in the search bar, this method gets called.
- (void)updateSearchResultsForSearchController:(UISearchController *)aSearchController 
    NSLog(@"updateSearchResultsForSearchController");

    NSString *searchString = aSearchController.searchBar.text;
    NSLog(@"searchString=%@", searchString);

    // Check if the user cancelled or deleted the search term so we can display the full list instead.
    if (![searchString isEqualToString:@""]) 
        [self.filteredItems removeAllObjects];
        for (NSString *str in self.allItems) 
            if ([searchString isEqualToString:@""] || [str localizedCaseInsensitiveContainsString:searchString] == YES) 
                NSLog(@"str=%@", str);
                [self.filteredItems addObject:str];
            
        
        self.displayedItems = self.filteredItems;
    
    else 
        self.displayedItems = self.allItems;
    
    [self.tableView reloadData];



    @end

【讨论】:

以上是关于UISearchBar 没有响应的主要内容,如果未能解决你的问题,请参考以下文章

视图加载时使 UISearchBar 成为第一响应者

UISearchBar 在尝试选择 UICollectionViewCell 时退出第一响应者

搜索栏没有响应 iOS

UISearchBar作为inputAccessoryView时的响应链

使用 UISearchBar 关闭键盘,而不让第一响应者辞职

第一响应者时 UISearchBar 上的平移手势