Tuesday, February 22, 2011

Dismiss Keyboard on TextFields, UITextView etc

Hi All,

To Dismiss the keyboard while using textFields and UITextViews follow the process below.

In you .h file

@interface youFileName : UIViewController UITextFieldDelegate, UIPickerViewDelegate, UITextViewDelegate,UIPickerViewDataSource, UIPickerViewDelegate> {

Use UITextField Delegate and UITextView Delegate so that you can dismiss as many keyboards for all respective TextFields and UITextView.



In your .m File Just copy and paste the following function


This function is for UITextView


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

if([text isEqualToString:@"\n"]) {

[textView resignFirstResponder];

return NO;

}

return YES;

}


This function is for UITextField


-(IBAction) textFieldReturn:(id) sender

{

[sender resignFirstResponder];

}



Finally to Make everything work, You need to open interface Builder and for each textfield and textView you use in your application You need to connect the delegate to files Owner.

Thats it build and run, Let me know if it works.

Thanks
Deepesh