刚才我点击按钮动态创建了文本字段 我的代码是:
-(void)clickonAdd
{
CGRect textfieldFrame = CGRectMake(18, 350,309,35.0);
UITextField *textField = [[UITextField alloc]initWithFrame:textfieldFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"Select Item List";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
}
现在我想添加更多不同位置的文本字段,点击同一个按钮...请你帮我...提前谢谢
答案 0 :(得分:1)
喜欢
适用于多个文字字段
float x = 20;
float y = 40;
for(int i=0;i<5;i++)
{
UITextfield *textField = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,200,30)];
textfield.tag = i;
// add your code here
[self.view addSubview:textField];
// if you want to change the y position of every textfield, change y coordinates
y = y+50;
// if you want to change the x position of every textfield, change x coordinates
x = x+50;
}
单一文字字段
我们可以采取多种方式。
<强>选择-1 强>
@interface SettingsViewController ()
{
// create two float values for x and y
CGFloat y;
CGFloat x;
UITextField *textField;
}
你的viewdidload上的
- (void)viewDidLoad {
[super viewDidLoad];
y = 0;
x = 0;
CGRect textfieldFrame = CGRectMake(x, y,309,35.0);
textField = [[UITextField alloc]initWithFrame:textfieldFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"Select Item List";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
// hide your textfield here
textField.hidden = YES;
}
按钮操作
-(void)clickonAdd
{
textField.hidden = NO;
x = x + 200;
y = y + 35;
textField.frame = CGRectMake(x, y, 309,35);
答案 1 :(得分:1)
试试这个......
@interface ViewController ()
{
CGFloat y;
CGFloat x;
}
- (void)viewDidLoad
{
[super viewDidLoad];
y = 0;
x = 0;
}
-(void)clickButtonAdd
{
UITextField *textField;
textField.frame = CGRectMake(x, y,100,30);
textField = [[UITextField alloc]initWithFrame:textfieldFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
x = x + 120;
y = y + 40;
}
答案 2 :(得分:0)
您可以执行以下操作:
-(void)clickonAdd:(CGRect)aFrame
{
CGRect textfieldFrame = aFrame;
// Rest of the code...
}
并在某处[self clickonAdd:CGRectMake(x, y, w, h)]
调用它。
答案 3 :(得分:0)
考虑使用UITableView。您将需要表视图,其中每个表视图单元格都保留UITextField。
然后在clickonAdd
方法中,您只需要将新单元格添加到表格视图中。
-(void) clickonAdd {
NSInteger numberOfRows = [self.textFieldsTableView numberOfRowsInSection:0];
NSIndexPath *newTextFieldIdxPath = [[NSIndexPath alloc] indexPathForRow: numberOfRows inSection:0];
[self.textFieldsTableView insertRowsAtIndexPaths:@[newTextFieldIdxPath] withRowAnimation: UITableViewRowAnimationAutomatic];
}