karl karl - 9 days ago 8
iOS Question

How to get or set value to specify UITextField?

Halo, I am a beginner in programming, and I start to learn Objective c now !

for (int i=0; i<9; i++){
y += 50;
x = 160;

for (int j=0; j<9; j++){
grid = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 50 , 50)];
grid.layer.borderWidth = 1;
grid.textAlignment = NSTextAlignmentCenter;
[[self view] addSubview:grid];

x += 50;

grid.text = [@(j) stringValue];

[initialArray addObject:grid];

}

}


I tried to using these code to for-loop many UITextField.

and each grid has a number, and I put the grid into a NSMutableArray

but how can I set a value to specify field? is it has a id for me to do this?
and how to get the value from field when the button is clicked?
thank you very much !!!

Answer

Setting a string

 grid.text=@""some value";
 [initialarray objectAtIndex:i].text=@"some value";

Getting value

 NSString *str=grid.text
 NSString *str=[initialarray objectAtIndex:i].text; 

You can set tag by simply giving

grid.tag=i; 
[initialarray objectAtIndex:i].tag=i;

i can be a loop variable.

Comments