vendredi 31 octobre 2014

Access text values from dynamically created UITextFields. iOS


Vote count:

0




I am creating UITextField 's in my view based on the count of an NSArray.


I want to know how I can access the text values based on the tag of the UITextField in my NSArray.


So after I add the below UITextField 's to my view how would I write code to retrieve .text values for myTextfield0, myTextfield1 and myTextfield2 respectively?


Example:



NSString *textFeildTextString = [NSString stringWithFormat:@"%@",myTextfield1.text];


Actual Code:



self.textFieldArray = [NSArray arrayWithObjects:@"one", @"two", @"three",nil];

NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
for (int i=0; i<[self.textFieldArray count]; i++) {

UIView *spacerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 60*i+150, 280, 45)];
myTextField.borderStyle = UITextBorderStyleNone;
myTextField.font = [UIFont systemFontOfSize:14];
myTextField.returnKeyType = UIReturnKeyDefault;
myTextField.placeholder = NSLocalizedString(@"Answer", nil);
myTextField.inputAccessoryView = [super createInputToolbar];
myTextField.background = [UIImage imageNamed:@"TextFieldBg"];
myTextField.textColor = [UIColor appTextColorGrey];
[myTextField setLeftView: spacerView1];
myTextField.leftViewMode = UITextFieldViewModeAlways;
myTextField.delegate = self;
myTextField.tag = 200+i;
[myDict setObject:myTextField forKey:[NSString stringWithFormat:@"myTextfield%d", i]];
[self.view addSubview:myTextField];
}

NSLog(@"Dict: %@",myDict);


The NSlog of my UITexfields



2014-10-31 11:58:32.715 [51992:13536821] Dict: {
myTextfield0 = "<UITextField: 0x7faea14f8430; frame = (20 150; 280 45); text = ''; clipsToBounds = YES; opaque = NO; tag = 200; layer = <CALayer: 0x7faea14f82d0>>";
myTextfield1 = "<UITextField: 0x7faea14fae70; frame = (20 210; 280 45); text = ''; clipsToBounds = YES; opaque = NO; tag = 201; layer = <CALayer: 0x7faea14faca0>>";
myTextfield2 = "<UITextField: 0x7faea16dce30; frame = (20 270; 280 45); text = ''; clipsToBounds = YES; opaque = NO; tag = 202; layer = <CALayer: 0x7faea16d19c0>>";


}



asked 29 secs ago







Access text values from dynamically created UITextFields. iOS

Aucun commentaire:

Enregistrer un commentaire