gpt4 book ai didi

ios - 如何关闭 ios 中自定义文本字段的键盘

转载 作者:行者123 更新时间:2023-11-29 13:11:17 25 4
gpt4 key购买 nike

我创建了 uitextfield 作为 uiimageview 的 subview ,textfield 和 uiimageview 都是 scrollview 的 subview 。在编辑文本字段时隐藏键盘的方法是什么。这是我的代码,它不起作用。

{
img1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 320, 60)];
img1.userInteractionEnabled=YES;
NSString *imgfilepath=[[NSBundle mainBundle]pathForResource:@"123" ofType:@"png"];
UIImage *imo=[[UIImage alloc]initWithContentsOfFile:imgfilepath];
[img1 setImage:imo];

UILabel *label1;
label1=[[UILabel alloc]init];
label1.frame=CGRectMake(60, 0, 250, 30);
label1.text=@"ENTER ANNUAL INCOME";
label1.textColor=[UIColor blackColor];
label1.font=[UIFont italicSystemFontOfSize:16.0f];

label1.backgroundColor=[UIColor clearColor];
[img1 addSubview:label1];


principal = [[UITextField alloc] initWithFrame:CGRectMake(85,30, 156, 40)];
principal.backgroundColor = [UIColor clearColor];
principal.clearButtonMode = UITextFieldViewModeWhileEditing;
principal.font = [UIFont systemFontOfSize:15.0f];
principal.placeholder=@"ENTER";
[principal setKeyboardType:UIKeyboardTypeNumberPad];
//principal.textAlignment=UITextAlignmentCenter;

[img1 addSubview:principal];

[img1 respondsToSelector:[principal resignFirstResponder]];

[scrollview addSubview:img1];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

[principal resignFirstResponder];

}

最佳答案

[img1 respondsToSelector:[principal resignFirstResponder]];

首先,respondsToSelector 返回一个 BOOL,您可以使用它来确定是否可以调用选择器。这不是 void 方法,看来您误解了这一点。

其次,UIImageView没有响应resignFirstResponder

第三,您正在将 void“传递”给 respondsToSelector:,令我惊讶的是编译结果?

最终您要做的是在您的 UITextField 上调用 resignFirstResponder,这将通过调用 [principal resignFirstResponder] 来实现。

但是,关闭键盘的最简单方法之一是调用 [self.view endEditing:YES],它被描述为...

Causes the view (or one of its embedded text fields) to resign the first responder status.

关于ios - 如何关闭 ios 中自定义文本字段的键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com