[英]how to dismiss keyboard of custom textfield in ios
我創建了uitextfield作為uiimageview的子視圖,並且textfield和uiimageview都是scrollview的子視圖。 編輯文本字段時隱藏鍵盤的方式是什么? 在這里我的代碼,它不起作用。
{
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
,您可以使用該BOOL
來確定是否可以調用選擇器。 這不是void
方法,似乎您誤解了該方法。
其次, UIImageView
不響應resignFirstResponder
。
第三,您正在“傳遞” void
到respondsToSelector:
,我對此感到驚訝嗎?
最終,您要做的是在UITextField
上調用resignFirstResponder
,這可以通過調用[principal resignFirstResponder]
來實現。
但是,解散鍵盤最簡單的方法之一是調用[self.view endEditing:YES]
,該操作被描述為...
使視圖(或其嵌入式文本字段之一)退出第一響應者狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.