I am looking for a way to identify the tag value of a UITextField. I created a button that appends the string "1". Please read the following so you can get a clear understanding of the issue.
Originally this was the code I had:
-(void)two:(id)sender {
NSString *myS = [NSString stringWithFormat:@"%@2", textField.text];
textField.text = myS;
}
The code above works if I have only one UITextField in the View (by connecting textField outlet to UITextField in the view). But the problem is that I have 6 textfields in the view, after doing some research I decided that using the tag value of the textfield would be the best solution. This is the code i tried to use:
-(IBAction) one:(id)sender {
NSInteger i = [sender tag];
switch (i) {
case 1:
myS = [NSString stringWithFormat:@"%@1", diameterTextField.text];
diameterTextField.text = myS;
break;
case 2:
myS = [NSString stringWithFormat:@"%@1", roughnessTextField.text];
roughnessTextField.text = myS;
break;
case 3:
myS = [NSString 开发者_开发知识库stringWithFormat:@"%@1", slopeTextField.text];
slopeTextField.text = myS;
break;
case 4:
myS = [NSString stringWithFormat:@"%@1", depthTextField.text];
depthTextField.text = myS;
break;
case 5:
myS = [NSString stringWithFormat:@"%@1", velocityTextField.text];
velocityTextField.text = myS;
break;
case 6:
myS = [NSString stringWithFormat:@"%@1", flowTextField.text];
flowTextField.text = myS;
break;
default:
break;
}
}
Using breakpoints I am noticing that the NSInteger i is being assigned the sender tag of the UIButton (IBAction one is connected to this button) not the UItextField. So my question is, is there any way to get the tag value of the text field the user tapped in? Once the user clicks the field and click on a UIButton I want the code to find the tag value and run through the switch statements.
I have read countless threads trying to find a solution, your help is much appreciated.
If you have any questions please feel free to ask.
Thanks, Dana
if ([myFirstTextField isFirstResponder]) {
i = myFirstTextField.tag;
}
else if ([mySecondTextField isFirstResponder]) {
i = mySecondTextField.tag;
}
...
This is the answer, I win! I am using this if statement to check to see if the current textfield isFirstResponder.
精彩评论