Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

winstonzeddemor

macrumors newbie
Original poster
Jan 8, 2010
6
0
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:

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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...but the user cannot select another UITextField without deselecting the current one, which can get annoying...
I'd say so!

Just so you know, it is possible to do this without requiring deselecting the current one. I know because that's what my app, a.k.a., does! Let me know if you want more details.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Attached is a sample project that shows three text fields and uses a background button to dismiss the keyboard. It also demonstrates "tabbing" between fields via the use of the 'return' button; only the last field has a Done button to dismiss the keyboard. Enjoy!
 

Attachments

  • Winston.zip
    20.7 KB · Views: 615
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.