Reputation: 95
just now i created text field dynamically,when i was clicked on button my code is :
-(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];
}
now i want to add more textfield with different positions,when clicked on same button...can u please help me...thanks in advance
Upvotes: 1
Views: 1918
Reputation: 1407
Consider using UITableView. You'll need table view, where each table view cell just keeps UITextField.
Then in clickonAdd
method you'll just need to add new cell into table view.
-(void) clickonAdd {
NSInteger numberOfRows = [self.textFieldsTableView numberOfRowsInSection:0];
NSIndexPath *newTextFieldIdxPath = [[NSIndexPath alloc] indexPathForRow: numberOfRows inSection:0];
[self.textFieldsTableView insertRowsAtIndexPaths:@[newTextFieldIdxPath] withRowAnimation: UITableViewRowAnimationAutomatic];
}
Upvotes: 0
Reputation: 944
Try this ...
@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;
}
Upvotes: 1
Reputation: 82756
do like
For Multiple textfield
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;
}
single textfield
we can do it multiple ways.
Choice-1
@interface SettingsViewController ()
{
// create two float values for x and y
CGFloat y;
CGFloat x;
UITextField *textField;
}
on your 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;
}
on button action
-(void)clickonAdd
{
textField.hidden = NO;
x = x + 200;
y = y + 35;
textField.frame = CGRectMake(x, y, 309,35);
Upvotes: 1
Reputation: 3659
You can do something like:
-(void)clickonAdd:(CGRect)aFrame
{
CGRect textfieldFrame = aFrame;
// Rest of the code...
}
And called it by [self clickonAdd:CGRectMake(x, y, w, h)]
somewhere.
Upvotes: 0