简体   繁体   中英

Hide UITextField in textFieldDidEndEditing

I'm trying to hide my UITextField in my app. I first tried to do it with a UIButton..

Therefor I made this:

-(IBAction)hide:(id)sender {

_textField.hidden = !_textField.hidden;

}

This worked but now I'm trying to do it automatically after editing so I wrote this code:

 -(void)textFieldDidEndEditing:(UITextField *)textField {

 textField.hidden = YES;

 }

In the .h I put this code:

-(void)textFieldDidEndEditing:(UITextField *)textField;

Could anyone tell me why this isn't working?

Is it possible to hide it with an animation? I tried this which wasn't working:

[UIView animateWithDuration:2.0 animations:^
{ code.. }
]

Thanks for helping me!

You should manipulate the alpha valeue of your textField to animate it For example:

textField.alpha = 1;
[UIView animateWithDuration:2.0 animations:^{
    textField.alpha = 0;
}];

The UITextField's hidden property is a BOOL and cannot be animated. Try using the alpha property of the text field. It accepts floating-point values, where 0.0 if fully transparent, and 1.0 is fully opaque.

So you could animate UITextField's alpha to hide it:

[UIView animateWithDuration: 2.0 
                 animations:^{ 
      textField.alpha = 0.0;
 }];

As beryllium said, you should also check if your text field actually has its delegate set up so that the -textFieldDidEndEditing: method gets called.

I assume you have a view controller set up something like this:

@interface ViewController : UIViewController<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

Then you should connect the UITextField's delegate outlet in your storyboard to the view controller like this:

在此输入图像描述

or you can do it programmatically in the -viewDidLoad method of your view controller:

- (void) viewDidLoad 
{
     self.textField.delegate = self;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM