gpt4 book ai didi

iphone - 如何在单个循环中添加多个 textField?

转载 作者:行者123 更新时间:2023-11-28 18:40:22 25 4
gpt4 key购买 nike

我想在一个循环中添加多个 UITextField。并在用户与 UITextFields 交互时识别它们。所以请帮助我。

谢谢

目前我单独放置 UITextField,像这样

txt_FirstName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_FirstName.borderStyle = UITextBorderStyleRoundedRect;
txt_FirstName.font = [UIFont systemFontOfSize:15];
txt_FirstName.placeholder = @"First Name";
txt_FirstName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_FirstName.keyboardType = UIKeyboardTypeDefault;
txt_FirstName.returnKeyType = UIReturnKeyDone;
txt_FirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_FirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_FirstName.delegate = self;
[frameView addSubview:txt_FirstName];
[txt_FirstName release];

framey1=framey1+50;

txt_MiddleName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_MiddleName.borderStyle = UITextBorderStyleRoundedRect;
txt_MiddleName.font = [UIFont systemFontOfSize:15];
txt_MiddleName.placeholder = @"Middle Name";
txt_MiddleName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_MiddleName.keyboardType = UIKeyboardTypeDefault;
txt_MiddleName.returnKeyType = UIReturnKeyDone;
txt_MiddleName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_MiddleName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_MiddleName.delegate = self;
[frameView addSubview:txt_MiddleName];
[txt_MiddleName release];

最佳答案

请使用它,但在您的 .h 文件中实现 UITextField 委托(delegate)。但是您应该根据您的要求更改占位符,例如 1 代表 FirstName,2 代表 MiddleName 等。

首先像这样创建通用函数,

-(void)setTextfieldStyle:(UITextField *)pTmpTextField
{
pTmpTextField.borderStyle = UITextBorderStyleRoundedRect;
pTmpTextField.font = [UIFont systemFontOfSize:15];
pTmpTextField.placeholder = @"First Name";
pTmpTextField.autocorrectionType = UITextAutocorrectionTypeNo;
pTmpTextField.keyboardType = UIKeyboardTypeDefault;
pTmpTextField.returnKeyType = UIReturnKeyDone;
pTmpTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
pTmpTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pTmpTextField.delegate = self;
}

并使用它来创建 UITextFiled,

int y = 0;
for(int i=0;i < 5;i++)
{
UITextField *txtTemp=[[UITextField alloc]initWithFrame:CGRectMake(0, y, 300, 31)];
txtTemp.tag = i;
[self setTextfieldStyle:txtTemp];
[self.view addSubview:txtTemp];
[txtTemp release];
y+=36;
}

这是 UITextField 的委托(delegate)方法,

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%@",textField.text);
}

希望这对你有用..

关于iphone - 如何在单个循环中添加多个 textField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11715539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com