创建动态textField

时间:2017-11-23 10:01:18

标签: ios

我正在开发IOS App。我动态创建TextField和按钮并设置标签值。但问题是当单击按钮获取显示为null的textfield第一个索引值时。只有textfield的最后一个索引值我得到的不是其他什么问题。在此先感谢。

Code..
- (void)addfields{
    _field = [[UITextField alloc]initWithFrame:CGRectMake(5.0f, 5.0f, 195.0f, 30.0f)];
    _field.layer.borderColor=[[UIColor colorWithRed:211.0f/255.0f
                                                         green:211.0f/255.0f
                                                          blue:211.0f/255.0f
                                                         alpha:1.0] CGColor];
    [_field.layer setBorderWidth: 1.0];
    //_field.tag = count;
    [_filterPossibleValueView addSubview:_field];

    _addField = [[UIButton alloc]initWithFrame:CGRectMake(202.0f, 5.0f, 30.0f, 30.0f)];
    _addField.backgroundColor = [UIColor blackColor];
    //_addField.tag = count;
    [_addField addTarget:self action:@selector(customFieldAdd:) forControlEvents:UIControlEventTouchUpInside];
    [_filterPossibleValueView addSubview:_addField];

}
- (IBAction)customFieldAdd:(id)sender{
    [_addfieldArray addObject:_field];

    [_scroller setScrollEnabled:YES];
    _scroller.contentSize=CGSizeMake(240.0f, _field.frame.size.height+x+50);

    [_copyfield removeFromSuperview];
    [copyAddButton removeFromSuperview];
    x = 10;
    y = 10;
    for( int i = 0; i < [_addfieldArray count]; i++ ) {
    _copyfield = [[UITextField alloc]initWithFrame:CGRectMake(5.0, x + _field.frame.size.height, 195.0f, 30.0f)];
    _copyfield.layer.borderColor=[[UIColor colorWithRed:211.0f/255.0f
                                              green:211.0f/255.0f
                                               blue:211.0f/255.0f
                                              alpha:1.0] CGColor];
    _copyfield.tag = i;
    [_copyfield.layer setBorderWidth: 1.0];
    [_filterPossibleValueView addSubview:_copyfield];
    x = x+_field.frame.size.height+10;

    copyAddButton = [[UIButton alloc]initWithFrame:CGRectMake(202.0f, y + _addField.frame.size.height, 30.0f, 30.0f)];
    copyAddButton.backgroundColor = [UIColor blueColor];
    copyAddButton.tag = i;
    [copyAddButton addTarget:self action:@selector(customFieldDelete:) forControlEvents:UIControlEventTouchUpInside];
    [_filterPossibleValueView addSubview:copyAddButton];
    y = y+_addField.frame.size.height+10;
    count++;
    }
}
- (IBAction)customFieldDelete:(id)sender{
    UIButton *button = (UIButton*)sender;
    NSInteger index = button.tag;
   [_addfieldArray removeObjectAtIndex:index];

//   UITextField *text = (UITextField *)[_copyfield viewWithTag:index];
//    NSLog(@"%ld",(long)text.tag);
    UITextField *txtField = [_filterPossibleValueView viewWithTag:index];
    NSLog(@"%@",txtField.text);

//     [_copyfield removeFromSuperview];
//     [copyAddButton removeFromSuperview];
}

1 个答案:

答案 0 :(得分:0)

我为动态字段创建了一个示例应用。在Button按钮上获取文本字段的值。此示例代码将解决您的问题。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

NSMutableArray *formArr; 
NSMutableArray *txtfldArr;



- (void)viewDidLoad {
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

formArr = [[NSMutableArray alloc] init];
txtfldArr = [[NSMutableArray alloc] init];
UITextField *txtfld = [[UITextField alloc] init];

NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:@"string" forKey:@"labelName"];
[dict1 setObject:@"name" forKey:@"labelFor"];
[dict1 setObject:@"1" forKey:@"tag"];
[dict1 setObject:txtfld forKey:@"txtFld"];
[formArr addObject:dict1];

NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
[dict2 setObject:@"string" forKey:@"labelName"];
[dict2 setObject:@"email" forKey:@"labelFor"];
[dict2 setObject:@"2" forKey:@"tag"];
[dict2 setObject:txtfld forKey:@"txtFld"];
[formArr addObject:dict2];

NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init];
[dict3 setObject:@"string" forKey:@"labelName"];
[dict3 setObject:@"phone" forKey:@"labelFor"];
[dict3 setObject:@"3" forKey:@"tag"];
[dict3 setObject:txtfld forKey:@"txtFld"];
[formArr addObject:dict3];

NSMutableDictionary *dict4 = [[NSMutableDictionary alloc] init];
[dict4 setObject:@"string" forKey:@"labelName"];
[dict4 setObject:@"address" forKey:@"labelFor"];
[dict4 setObject:@"4" forKey:@"tag"];
[dict4 setObject:txtfld forKey:@"txtFld"];
[formArr addObject:dict4];

int y = 70;

for (int i = 0; i < [formArr count]; i++) {

    NSString *txtfldName = [NSString stringWithFormat:@"%@",[[formArr objectAtIndex:i] objectForKey:@"labelFor"]];

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(30, y+4, 80, 25)];
    lbl.text = txtfldName;
    lbl.textColor = [UIColor darkGrayColor];
    [self.view addSubview:lbl];

    UITextField *lbl_txtfld = [[UITextField alloc] initWithFrame:CGRectMake(120, y, 150, 30)];
    lbl_txtfld.text = @"";
    lbl_txtfld.delegate = self;
    lbl_txtfld.textColor = [UIColor whiteColor];
    lbl_txtfld.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lbl_txtfld];

    [[formArr objectAtIndex:i] setObject:lbl_txtfld forKey:@"txtFld"];
    y += 40;
}

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];
return YES;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)buttonClicked:(id)sender {

for (int i = 0; i < [formArr count]; i++) {

    NSString *labelName = [NSString stringWithFormat:@"%@",[[formArr 
objectAtIndex:i] objectForKey:@"labelFor"]];

    UITextField *txtfld = [[formArr objectAtIndex:i] 
objectForKey:@"txtFld"];
    NSLog(@"txtfld value for %@ = %@",labelName,txtfld.text);
}
}