user2507735
user2507735

Reputation: 15

how to dismiss keyboard of custom textfield in ios

I have created uitextfield as a subview of uiimageview & both textfield and uiimageview are subview of scrollview. what is the way to hide the keyboard while editing the textfield. here my codes , its not working.

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

}

Upvotes: 1

Views: 224

Answers (1)

Chris Wagner
Chris Wagner

Reputation: 21013

[img1 respondsToSelector:[principal resignFirstResponder]];

First of all, respondsToSelector returns a BOOL which you use to determine if you can call a selector. This is not a void method, it appears that you have misunderstood that.

Second, UIImageView does not respond to resignFirstResponder.

Third, you are "passing in" void to respondsToSelector:, which I am surprised compiles?

Ultimately what you want to do is call resignFirstResponder on your UITextField which would be achieved by calling [principal resignFirstResponder].

But, one of the easiest ways to dismiss the keyboard is by calling [self.view endEditing:YES] which is described to do ...

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

Upvotes: 1

Related Questions