I have The following code…
for (j=0; j<20; j++) {
if (j<20) {
txtField = [[UITextField alloc] initWithFrame:CGRectMake(110, txt_Pos_Y, 100, 15)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.font = [UIFont systemFontOfSize:11];
txtField.tag = j;
txtField.placeholder = @"Enter Value";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtField.delegate = self;
[scrollView addSubview:txtField];
[txtField release];
txt_Pos_Y = txt_Pos_Y + 20;
}
The code creates 20 textfields….All Textfields are editable when view will load.Now I want to give value.I want, on which textfield I click First, only that TextField will be editable and Rest will be NonEditable.
I gave the tag , txtField.tag = j;
now i am not able to figure out that which tag is pressed or clicked? And not able To make the TextField editable which i clicked First, and rest Non-Editable?
I tried this to check the condition in CalculateMethod() method that txtField.tag == 1 is clicked but this is not working.
-(void)CalculateMethod {
UITextField *txtFld = (UITextField*)[scrollView viewWithTag:1];
// I want if TextFeild "txtField.tag == 1" is clicked then Enter this condition
if(txtField.tag == 1){
double a;
a = Var_sqFt * Var_sqMtr;
txtFld.text = [[NSString alloc] initWithFormat:@"%.4f",a];
}
// I want if TextFeild "txtField.tag == 2" is clicked then Enter this condition
if(txtField.tag == 2){ // Here I want Only that
double a;
a = Var_sqFt * Var_sqMtr;
txtFld.text = [[NSString alloc] initWithFormat:@"%.4f",a];
}
}
So I have 2 questions
-
I want, on which textfield I click First, only that TextField will be editable and Rest will be NonEditable , how to do this?
-
Which TextField is clicked/Touch/Pressed , Like I tried in CalculateMethod() method?
To check which field started editing
You should look into the
Delegatemethods for yourUITextFields. Simply set the delegate of all fields toselfi.e. the controller creating the textFields. Then implement theUITextFielddelegate methodin the method check the tag of incoming parameter
textFieldto perform your task.To enable disable fields
Put all your fields in an
NSMutableArray, iterate the array and only enable the field which matches the delegate parameter. Disable the rest.