DB Cooper
DB Cooper

Reputation: 21

Click textfield and button disappears (it doesn't, but i want it to)

I have a textfield and a button. When I click inside the textfield, I want the button to disappear. I defined the textfield as both outlet and action ( with event “Did end on exit”). In the method for the textfield, I have self.testButton.hidden = YES; When I click inside the textfield, the button does not go away. Instead, it remains until I hit the return key on the keyboard – causing the keyboard to go away. I tried the same thing w/ touchup inside as the event on the text field. When you click in the text field, nothing happens to the button.

Upvotes: 1

Views: 147

Answers (3)

LuisCien
LuisCien

Reputation: 6432

Instead of using the Target-Action mechanism ("Did end on exit" and "Touch Up Inside") use the Delegate mechanism.

First, make your class conform to the UITextFieldDelegate protocol. In your *.h (header) file add the following:

// Here I'm assuming your class is inheriting from UIViewcontroller but it
// may be inheriting from some other class. The really important part here 
// is: <UITextFieldDelegate>. That's how you make your class conform to that protocol

@interface THE_NAME_OF_YOUR_CLASS : UIViewController <UITextFieldDelegate>

.

Second, implement the -(void)textFieldDidBeginEditing:(UITextField *)textField method. Also, remember to set yourself as the delegate too: self.textField.delegate = self. That way, the method will get called every time the user starts editing. Inside that methdod call self.testButton.hidden = YES;. In your *.m (implementation) file add the following:

-(void)viewDidLoad {

    // here I'm assuming you have a 'strong' reference to your text field. 
    // You're going to need one to set yourself as the delegate.
    self.textField.delegate = self;
}

// This is one of the methods defined in the UITextFieldDelegate protocol
-(void)textFieldDidBeginEditing:(UITextField *)textField { 

    self.testButton.hidden = YES;
}

.

Similarly, to make your button appear again, implement the - (void)textFieldDidEndEditing:(UITextField *)textField method. Inside it un-hide your button. Again, in your *.m file add the following:

// This is another method defined in the UITextFieldDelegate protocol
-(void)textFieldDidEndEditing:(UITextField *)textField {

    self.testButton.hidden = NO;
}

Although delegates may be a mystery to you right now once you become familiar with them you will realize they're very easy. And this is very important because iOS programming relies heavily on delegates.

A delegate is a "notification" mechanism based on the "Hollywood" principle which is: don't call us; we'll call you. In your case the class that contains the UITextField is interested in knowing when the UITextField begins editing and when it ends editing. But your class cannot be "polling" (that is, constantly asking) the text field to find out if the state changed. Instead you register your class with the text field and it will be the text field the one that will let you know when something happened. That will be thanks to the methods that you implemented.

Further reading: protocols and delegates

Hope this helps!

Upvotes: 1

thelaws
thelaws

Reputation: 8001

If you want to button to disappear when the user begins editing the text field, try UIControlEventEditingDidBegin.

Upvotes: 0

Callum Abele
Callum Abele

Reputation: 71

Have you made sure that testButton has its IBOutlet set before you hide it?

Upvotes: 0

Related Questions