1

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

{
   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];

}
4

1 回答 1

1

[img1 respondsToSelector:[principal resignFirstResponder]];

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

UIImageView是不回应resignFirstResponder

第三,您正在“传递”voidrespondsToSelector:,我很惊讶编译?

最终你想要做的是调用resignFirstResponderUITextField的调用来实现[principal resignFirstResponder]

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

导致视图(或其嵌入的文本字段之一)放弃第一响应者状态。

于 2013-06-27T23:58:41.907 回答