寻找当前 UITextField 的标签值,UIView 中的多个 UITextField

发布于 2024-10-31 05:50:23 字数 1598 浏览 1 评论 0原文

我正在寻找一种方法来识别 UITextField 的标签值。我创建了一个附加字符串“1”的按钮。请阅读以下内容,以便您能够清楚地了解该问题。

最初这是我的代码:

-(void)two:(id)sender {
NSString *myS = [NSString stringWithFormat:@"%@2", textField.text];
textField.text = myS;

}

如果视图中只有一个 UITextField(通过将 textField 出口连接到视图中的 UITextField),上面的代码就可以工作。但问题是我的视图中有 6 个文本字段,经过一些研究后,我决定使用文本字段的标签值将是最好的解决方案。这是我尝试使用的代码:

-(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;
}

}

使用断点我注意到 NSInteger i 被分配给 UIButton 的发送者标签(IBAction 一个连接到此按钮)而不是 UItextField。所以我的问题是,有没有办法获取用户点击的文本字段的标签值?一旦用户单击该字段并单击 UIButton,我希望代码找到标记值并运行 switch 语句。

我已经阅读了无数的帖子试图找到解决方案,非常感谢您的帮助。

如果您有任何疑问,请随时询问。

谢谢, 达纳

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-11-07 05:50:23
if ([myFirstTextField isFirstResponder]) {
   i = myFirstTextField.tag;
}
else if ([mySecondTextField isFirstResponder]) {
   i = mySecondTextField.tag;
}
...

这就是答案,我赢了!我正在使用此 if 语句来检查当前文本字段是否为FirstResponder。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文