【发布时间】:2015-04-04 17:35:09
【问题描述】:
我有一个带有 UITextField、UIButton 和 UITable 视图的 UIView。文本字段和按钮会影响搜索栏,然后将结果加载到表格视图中。
我想让他们键盘消失。如果用户在编辑文本字段时点击了某些东西。我的策略是在 UIView 中添加一个手势识别器,但是手势识别器似乎会拦截来自表格视图的所有触摸,而您 |tableView:didSelectCellAtIndexPath:|永远不会被调用。有趣的是(至少对我而言)是 UIButton 在用户编辑字段时仍然可以点击,即使 UITableView 不是。
我已经尝试实现 |gestureRecognizer::shouldRecognizeSimultaneouslyWithGestureRecognizer:|总是返回是,但这无济于事。我也试过设置
singleTapRecognizer.cancelsTouchesInView = NO;
这也无济于事。
我很高兴在文本字段调用 |textFieldDidBeginEditing:| 时添加和删除手势识别器。和 |textFieldDidFinishEditing:|,虽然这感觉很混乱,并且在编辑文本字段时仍然需要点击两次才能触摸单元格(一次关闭键盘并移除识别器,一次点击单元格)。
有没有更好的办法?
相关代码如下:
- (void)loadView {
[super loadView];
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollView.backgroundColor = [FDEColors viewBackgroundColor];
self.view = self.scrollView;
self.searchField = [[UITextField alloc] initWithFrame:CGRectZero];
self.searchField.placeholder = @"What are you looking for?";
self.searchField.backgroundColor = [FDEColors textFieldBackgroundColor];
self.searchField.clipsToBounds = YES;
self.searchField.layer.borderColor = [[FDEColors buttonColor] CGColor];
self.searchField.layer.borderWidth = 1.f;
self.searchField.returnKeyType = UIReturnKeySearch;
self.searchField.delegate = self;
[self.view addSubview:self.searchField];
self.searchButton = [[UIButton alloc] initWithFrame:CGRectZero];
self.searchButton.backgroundColor = [FDEColors buttonColor];
[self.searchButton setTitle:@"Search" forState:UIControlStateNormal];
[self.searchButton addTarget:self
action:@selector(searchPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.searchButton];
self.resultsTableView =
[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.resultsTableView.delegate = self;
self.resultsTableView.dataSource = self;
self.resultsTableView.backgroundColor = [FDEColors viewBackgroundColor];
[self.resultsTableView setSeparatorInset:UIEdgeInsetsZero];
self.resultsTableView.layoutMargins = UIEdgeInsetsZero;
[self.resultsTableView registerClass:[FDESearchResultsCell class]
forCellReuseIdentifier:[FDESearchResultsCell reuseIdentifier]];
[self.view addSubview:self.resultsTableView];
self.singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:self.singleTapRecognizer];
}
- (void)dismissKeyboard {
[[self view] endEditing:YES];
}
【问题讨论】:
标签: ios objective-c uitableview uiscrollview