我知道这是一个非常基本的概念,但由于某些原因,当用户在我的视图控制器中点击两个UITextField
之外时,我无法解除键盘。我已经尝试了很多我能找到的解决方案,而且没有一个正在工作,这告诉我,我在某种程度上误解了它们。这是我初始化UITextField
s:
// Min Price
//UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 25, 70, 30)];
tf.userInteractionEnabled = YES;
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(125, 25, 70, 30)];
tf1.userInteractionEnabled = YES;
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
//and so on adjust your view size according to your needs
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(70, 100, 200, 60)];
[view addSubview:tf];
[view addSubview:tf1];
[self.view bringSubviewToFront: tf];
[self.view bringSubviewToFront: tf1];
[self.view addSubview:view];
这是我用来解雇键盘的功能:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
答案 0 :(得分:0)
您只是查看视图的直接子视图;您的文本字段都是另一个级别(视图的子视图的子视图)。有很多方法可以解决这个问题。最简单的方法是标记两个视图,然后按标记获取它们;你也可以保留一个指向活动字段(或两者)的指针,然后将其解除/它们。
答案 1 :(得分:0)
最简单的解决方案是拥有一个不可见的uiview,为其启用触摸交互,只需检测何时触摸此视图以隐藏自身并调用resignFirstResponder。将视图放在视图层次结构下面的文本字段中,它应该可以工作。您甚至不需要知道哪个文本字段处于活动状态,因为所有文本字段都会隐藏。
答案 2 :(得分:0)
正如其他人所说,我的子视图的设置结果是问题,将其更改为下面的代码,现在可以正常工作了。
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 125, 70, 30)];
[self.view addSubview:tf];
tf.userInteractionEnabled = YES;
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(125, 125, 70, 30)];
[self.view addSubview:tf1];
tf1.userInteractionEnabled = YES;
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
答案 3 :(得分:-1)
点击外部视图,将按钮大小设置为整个视图,然后单击此按钮编写此代码。
[self.view endEditing:YES];