Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?
Source: Tips4all
Source: Tips4allSource: CCNA FINAL EXAM
Cisco Certified Network Associate Exam,640-802 CCNA All Answers ~100/100. Daily update
Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?
use tochesBegin method for this purpose
ReplyDelete- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[yourFirstTextField resignFirstResponder];
[yourSecondTextField resignFirstResponder];
}
You do not need any thing else.When you touch anywhere on view then both textField resign no need to to know which one having focus.and when you touch textField then textField open keyboard by own.
The answer by Ishu is a good one. But I think you should also give a try to :
ReplyDelete- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Create an invisible button, put it behind the UITextField and send resignFirstResponder message to your text fields when the button is tapped.
ReplyDeleteUse delegate,
ReplyDelete- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
You can also create a method in your controller
-(IBAction)editingEnded:(id)sender{
[sender resignFirstResponder];
}
and then in Connection Inspector in IB connect Event "Did End On Exit" to it.
You need to create a custom button at the background and then connect it to the IBAction method that calls resignFirstResponder on the your UITextField
ReplyDeletesomething like this
Edit:
as you want to add the button programmatically than you can use this code
- (void)viewDidLoad
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
button.buttonType = UIButtonTypeCustom;
[button addTarget:self action:(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
and than add your controller code after this.
Keep a reference to the UITextField in your view controller and call [yourTextField resignFirstResponder]
ReplyDeleteYou need to define the action for your button click this way
ReplyDelete[infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
And implement the click method ,as for example i have provided the animation on button click method.
-(void)backButtonClicked
{
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[self.navigationController popViewControllerAnimated:YES];
[UIView commitAnimations];
}
Hope this will help you ,the action is without using IB.Its all done through coding
If you are not using Interface builder, you can manually hook up events as explained here:
ReplyDeleteHow can I programmatically link an UIView or UIImageView with an event like "touch up inside"?
If you are using Interface builder, then you don't need to create a background button.
You could do this:
Select your view (probably called View)
Change to the (i) tab in the property inspector
Change the class from UIView to UIControl (this gives you access to the Touch Down event).
Hook the Touch Down event to your backgroundTap method (Ctrl + drag).
Don't forget to Save changes