I have searched many times for this and there does not seem to be a good thread with the answers in it, so here is how I did it. I hope it helps someone.
In the UITextFieldDelegate header file, create a pointer to, among other things, a UIButton:
First, re-implement the textFieldShouldBeginEditing function. Bring the UITextField that is being edited to the front and add a semi-transparent button behind it, this way any touch events outside of the UITextField will be received by the new button:
The buttonPressed function should look for the '999' tag that is assigned to the new button, and then resign some first responders. At this point, I just had the button resign all of the first responders for all of the text fields in the view:
The textFieldShouldReturn function must also be altered, so that when a user presses the "Done" key on the keyboard, the releaseButton is hidden and released.
That works for my app, but the user cannot select another UITextField without deselecting the current one, which can get annoying...but I guess
UITextFields are already pretty annoying.
In the UITextFieldDelegate header file, create a pointer to, among other things, a UIButton:
Code:
..
//a view
UIView *myView;
//some UITextFields
UITextField field1;
UITextField field2;
UITextField field3;
UITextField field4;
//a button
UIButton *releaseButton;
..
First, re-implement the textFieldShouldBeginEditing function. Bring the UITextField that is being edited to the front and add a semi-transparent button behind it, this way any touch events outside of the UITextField will be received by the new button:
Code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//bring the text field to the front
[myView bringSubviewToFront:textField];
//create a custom button
releaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
// make it the size of the screen
releaseButton.frame = CGRectMake(0, 0, 320, 480);
//give it a tag that you can use later
releaseButton.tag = 999;
//it needs no title
[releaseButton setTitle:@"" forState:UIControlStateNormal];
//set it to black
releaseButton.backgroundColor = [UIColor blackColor];
//now it will appear to darken the rest of the view
releaseButton.alpha = 0.6;
//make it call buttonPressed when you touch it
[releaseButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
// add it to myView one object beneath the current text field
[myView insertSubview:releaseButton belowSubview:textField];
return YES;
}
The buttonPressed function should look for the '999' tag that is assigned to the new button, and then resign some first responders. At this point, I just had the button resign all of the first responders for all of the text fields in the view:
Code:
-(IBAction)buttonPressed:(id)sender{
if ([sender isKindOfClass:[UIButton class]]){
int myTag = [(UIButton *)sender tag];
if(myTag == 999){
//resign all of the text fields first responders
[field1 resignFirstResponder];
[field2 resignFirstResponder];
[field3 resignFirstResponder];
[field4 resignFirstResponder];
//hide and release the button
if(releaseButton != nil){
releaseButton.hidden=YES;
[releaseButton removeFromSuperview];
releaseButton=nil;
}
}
}
}
The textFieldShouldReturn function must also be altered, so that when a user presses the "Done" key on the keyboard, the releaseButton is hidden and released.
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(releaseButton != nil){
releaseButton.hidden=YES;
[releaseButton removeFromSuperview];
releaseButton=nil;
}
[textField resignFirstResponder];
return YES;
}
That works for my app, but the user cannot select another UITextField without deselecting the current one, which can get annoying...but I guess
UITextFields are already pretty annoying.