I have five textfields
and one animation method. I want to call animation method only once on each click of a textfield
(so basically five times totally and each textfield
only once). I have tried it but unable to figure out the proper way to do it. Please help me. Any kind of help is appreciated.Thanks.
You will need to take five boolean variables for each textField.
BOOL flag1;
BOOL flag2;
BOOL flag3;
BOOL flag4;
BOOL flag5;
By default it's value is false. So make it true on textField's delegate method textFieldShouldBeginEditing as below, Also assign tags 1,2,3..,5 to each textFields.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField.tag == 1) {
if(flag1 == FALSE) {
//Call Animation method over here..
}
flag1 = TRUE;
} else if (textField.tag == 2) {
if(flag2 == FALSE) {
//Call Animation method over here..
}
flag2 = TRUE;
} else if //.......... and so on for other three textfields..
}
If I understand correctly, you may just want to set some type of flag that is set on the button click for each of the five buttons. So if button one was clicked, set button one flag to yes. Then make sure your animation only triggers if the button flag = no.
You will need five properties in your Controller:
var isTextField1Played = false
var isTextField2Played = false
var isTextField3Played = false
var isTextField4Played = false
var isTextField5Played = false
and in the click method, write:
func click1() {
if isTextField1Played == true {
return
}
//Animation code
isTextField1Played = true
}
While the accepted answer is correct and working it gets a little tedious to manage and unneccesarily enlarges your view controller code. Another approach would be to subclass UITextField
: even something as simple like this :
CustomTextField.h :
#import <UIKit/UIKit.h>
@interface CustomTextField : UITextField
@property (nonatomic, assign) BOOL hasAlreadyAnimated;
@end
CustomTextField.m :
#import "CustomTextField.h"
@implementation CustomTextField
@end
The implementation file can actually be left empty, as we only care about the added property, and it will be initialized to NO
by default.
This enables to reduce your delegate method to this :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isKindOfClass:[CustomTextField class]) { // sanity check, also needed if you want to have some non-animatable text fields
CustomTextField *customTF = (CustomTextField *)textField;
if (customTF.hasAlreadyAnimated == NO) {
//your animation code
}
customTF.hasAlreadyAnimated = YES;
}
}
Another benefit from this approach is that if you decided to change the number of animatable text fields you don't need to add/remove these flags.
This class can be used both in code and in Interface Builder - check this thread if you don't know how.
Assign tags 1,2,3,4,5..etc for textfields in storyboard and check condition like below and assign new tag value. With below code you don't any extra variables or new custom class.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField.tag < 100) { //if you think there may be possibility for more than 100 text fields you can change.
textField.tag += 100;
//call Animation method here.
}
}
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.