[英]Keyboard on UITextField dismisses but cannot click other UITextField
[英]UITextField show keyboard on tableviewcell click
我有一個具有標簽和文本字段的簡單自定義表格視圖單元格。 在故事板上看起來像這樣:
我想在用戶單擊單元格中的任何位置(包括他們單擊標簽)時顯示鍵盤。 我99%的人確定要實現此目標的方法是在單擊該單元格時調用成為firstFirstResponder。
這是我簡單的ViewController:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView setEstimatedRowHeight:44.0f];
[self.tableView setRowHeight:UITableViewAutomaticDimension];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custom"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
BOOL firstResponder = [cell becomeFirstResponder];
}
我的自定義表格視圖單元格:
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (BOOL) becomeFirstResponder {
return [self.textField becomeFirstResponder];
}
@end
我驗證了成為beginFirstResponder的調用,但是返回的是false。 我想念什么?
就像@ alex-i在這里的評論中指出的那樣:
當文本字段在成為第一響應者的同時從視圖/窗口層次結構中短暫刪除時(例如,重新加載保存該文本字段的UITableView或UICollectionView),也會發生[文本字段未成為第一響應者]的情況。
這將在選擇時發生。
您可以使用以下操作將UITapGestureRecognizer添加到tableView中,而不是使用didSelectRowAtIndexPath:
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
BOOL firstResponder = [cell becomeFirstResponder];
}
它將成為第一響應者。
如果有人需要快速的替代方法:您可以將外部UIView與插座集合連接。 在每個UIViewController類上,您都可以在下面調用一次。 因此,當觸摸外框時,將使用鍵盤激活editfield。 您也可以將其應用於標簽。 (通過禁用標簽的用戶互動)
@IBOutlet var outerBoxes:[UIView]!
override func viewDidLoad() {
super.viewDidLoad();
for outerBox in outerBoxes { outerBox.respondForEditBox() };
...
但是一旦您必須擁有以下代碼:
extension UIView {
func respondForEditBox() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIView.focusOnEditBox));
self.addGestureRecognizer(tap);
}
func focusOnEditBox() {
for sview in self.subviews {
if sview is UITextField {
sview.becomeFirstResponder();
}
}
}
}
受到@Esqarrouth在“ 關閉iOS鍵盤”上的回答的啟發,方法是使用Swift觸摸任意位置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.