Search code examples
iphonexcodeuicontrolkeypadresignfirstresponder

IPhone app xcode, trying to hide text input by clicking on the background?


I'm following an example in Beginning Iphone Development 4 for basic user interaction.

I'm using xcode 4.02 with sdk 4.3

I have a basic viewcontroller.xib with two textbox's, one for numeric values and another for text. Once I'm finished editing either of the text fields I wish to tap the background and have the keypad disappear.

The books says to change the container UIView object to a UIControl and then add an action onto the UIControl event Touch Down. Then create a method to resign the first responder of each of the textbox's and link this to the touch down event. I've done this. Here is my code, is there anything I've missed? At the moment nothing happens.

enter image description here Thanks

@interface ControlFunViewController : UIViewController {
    UITextField *nameField;
    UITextField *numberField;
}

@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *numberField;

- (IBAction)textFieldDoneEditing:(id)sender;
- (IBAction)backgroundTap:(id)sender;

Implementation:

#import "ControlFunViewController.h"

@implementation ControlFunViewController

@synthesize nameField;
@synthesize numberField;

- (void)dealloc
{
    [nameField release];
    [numberField release];
    [super dealloc];
}

- (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender {
    [nameField resignFirstResponder];
    [numberField resignFirstResponder];
}

Solution

  • Found the answer, I didn't link up the outlets for the text boxes so the controller didn't no they existed. School boy error.

    http://www.iphonedevbook.com/forum/chapter-4-more-user-interface-fun/4145-backgroundtap.html