IOS-当触摸UITextField外部时关闭键盘

IOS-当触摸UITextField外部时关闭键盘

我想知道当用户触摸到UITextField.


慕田峪7331174
浏览 360回答 3
3回答

jeck猫

您需要添加一个UITapGestureRecogniser并将其分配到视图中,然后在UITextField上面是选择器。守则:视图DidLoadUITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];[self.view addGestureRecognizer:tap];解雇键盘:-(void)dismissKeyboard  {     [aTextField resignFirstResponder];}(在哪里aTextField是负责键盘的文本字段)SWIFT 3版本看起来是这样的let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))self.view.addGestureRecognizer(tapGesture)解雇键盘func dismissKeyboard (_ sender: UITapGestureRecognizer) {     aTextField.resignFirstResponder()}

翻过高山走不出你

我想出了几个答案。使用在viewDidLoad:UIGestureRecognizer *tapper;- (void)viewDidLoad{     [super viewDidLoad];     tapper = [[UITapGestureRecognizer alloc]                 initWithTarget:self action:@selector(handleSingleTap:)];     tapper.cancelsTouchesInView = NO;     [self.view addGestureRecognizer:tapper];}删除当前正在编辑的内容:- (void)handleSingleTap:(UITapGestureRecognizer *) sender{     [self.view endEditing:YES];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS